make a block play a sound randomly

User avatar
Joseph16
Member
 
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

make a block play a sound randomly

by Joseph16 » Sun May 12, 2019 16:31

I need to make it so every few seconds at random times, my node will emit a sound to anyone within a few blocks, say 4. How do I do this? I read the wiki and documentation but was pretty confused.
Testin' mines since 1989
 

User avatar
Linuxdirk
Member
 
Posts: 2178
Joined: Wed Sep 17, 2014 11:21
Location: Germany
In-game: Linuxdirk

Re: make a block play a sound randomly

by Linuxdirk » Mon May 13, 2019 10:59

You need to define it in the node’s definition.

Code: Select all
minetest.register_node('test:test', {
    description = 'Test',
    tiles = { 'default_wood.png^[colorize:#ffff00:128' },
    groups = { oddly_breakable_by_hand = 1 },

    -- Execute when the node was placed
    --
    -- https://github.com/minetest/minetest/blob/568694122cb662d6c6cb071338c9d30721372b50/doc/lua_api.txt#L6241-L6246
    after_place_node = function(pos)
        local timer = minetest.get_node_timer(pos) -- Get the node timer
        timer:start(4) -- Set it to 4 seconds
        return true -- Do everything else that happens after placing a node
    end,

    -- Execute when the timer reached the given duration.
    --
    -- https://github.com/minetest/minetest/blob/568694122cb662d6c6cb071338c9d30721372b50/doc/lua_api.txt#L6274-L6279
    on_timer = function(pos)
        local timer = minetest.get_node_timer(pos) -- Get the node timer

        -- Play a random sound named like so:
        --
        -- mymod_mysound.1.ogg
        -- mymod_mysound.2.ogg
        -- mymod_mysound.3.ogg
        -- mymod_mysound.N.ogg
        -- ...
        --
        -- placed in ./mymod/sounds
        --
        -- https://github.com/minetest/minetest/blob/568694122cb662d6c6cb071338c9d30721372b50/doc/lua_api.txt#L789-L814
        minetest.sound_play('mymod_mysound', {
            pos = pos, -- Source of the sound
            max_hear_distance = 4 -- Distance in nodes to hear the sound
        })

        timer:start(4) -- Start the timer again
    end
})
 

User avatar
Joseph16
Member
 
Posts: 308
Joined: Tue Dec 06, 2016 05:35
In-game: Joseph16

Re: make a block play a sound randomly

by Joseph16 » Mon May 13, 2019 12:33

Should I then add something like
Code: Select all
on_place
Mintest.play_sound
or something like that?
Testin' mines since 1989
 

User avatar
Linuxdirk
Member
 
Posts: 2178
Joined: Wed Sep 17, 2014 11:21
Location: Germany
In-game: Linuxdirk

Re: make a block play a sound randomly

by Linuxdirk » Mon May 13, 2019 16:49

Joseph13 wrote:Should I then add something like
Code: Select all
on_place
Mintest.play_sound
or something like that?

Read again what I wrote.
 


Return to Modding Discussion



Who is online

Users browsing this forum: No registered users and 7 guests