Page 1 of 1

How do I play a sound when a player jumps?

PostPosted: Fri Mar 01, 2013 03:48
by ryguy
I'm trying to implement a mod where a certain sound is played whenever a player jumps - guaranteed. I figured out how to respond to it by searching for players with the jump key pressed every few hundredths of a second, but if the player presses the jump key quickly it may be missed. Also, there is usually a delay between when the player jumps and when the sound is played. This is my current code:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local timer = -1
minetest.register_globalstep(function (dtime)
    if timer == -1 then
        for _,player in ipairs(minetest.get_connected_players()) do
            local ctrl = player:get_player_control()
           
            if ctrl.jump then
                print("Player jumped!") -- I haven't built the code to actually play the sound yet...
                timer = 0
            end
        end
    else
        timer = timer + dtime
        if timer >= 0.65 then -- this is a fudged number
            timer = -1
        end
    end
end)


What I am hoping for is something similar to this:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
player.on_jump = function(player)


Where the function is called when the player jumps, every time.

PostPosted: Fri Mar 01, 2013 04:35
by prestidigitator
Unfortunately there isn't anything like this. There are very, very few hooks for players. You can tell when the join, leave, are first added to a server, die, and (re)spawn. I believe that's it. Everything else is focused on nodes, Lua entities, and detached inventories. So you'll have to hack it together as you indicated, with timers and such. I really hope we see more enhancements around players and entities in general in future versions.

PostPosted: Fri Mar 01, 2013 09:20
by Calinou
That kind of sound should be added in the client C++ code, else there would be latency between the player jumping and the sound being played.

PostPosted: Fri Mar 01, 2013 17:45
by ryguy
That sounds like what I am looking for. I have some experience in C++, but not while working with Minetest. Is there a tutorial for modifying the C++ code in Minetest?

PostPosted: Fri Mar 01, 2013 21:02
by RealBadAngel

PostPosted: Fri Mar 01, 2013 21:25
by 4aiman
You can try to get player position and play a sound if there's air node under him.
Without C++ you can't make that better than this.

PostPosted: Fri Mar 01, 2013 21:38
by ryguy
4aiman wrote:You can try to get player position and play a sound if there's air node under him.
Without C++ you can't make that better than this.


Unfortunately that would play the sound when a player was falling as well as jumping... I'm looking into the C++ solution now.

PostPosted: Fri Mar 01, 2013 21:40
by 4aiman
ryguy wrote:
4aiman wrote:You can try to get player position and play a sound if there's air node under him.
Without C++ you can't make that better than this.


Unfortunately that would play the sound when a player was falling as well as jumping... I'm looking into the C++ solution now.

Like I didn't know that, dude ;)