How do I play a sound when a player jumps?

ryguy
New member
 
Posts: 3
Joined: Fri Mar 01, 2013 03:37

How do I play a sound when a player jumps?

by ryguy » Fri Mar 01, 2013 03:48

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.
 

prestidigitator
Member
 
Posts: 632
Joined: Thu Feb 21, 2013 23:54

by prestidigitator » Fri Mar 01, 2013 04:35

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.
 

User avatar
Calinou
Member
 
Posts: 3124
Joined: Mon Aug 01, 2011 14:26
GitHub: Calinou
IRC: Calinou
In-game: Calinou

by Calinou » Fri Mar 01, 2013 09:20

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.
 

ryguy
New member
 
Posts: 3
Joined: Fri Mar 01, 2013 03:37

by ryguy » Fri Mar 01, 2013 17:45

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?
 

User avatar
RealBadAngel
Member
 
Posts: 556
Joined: Wed Jul 18, 2012 16:30

by RealBadAngel » Fri Mar 01, 2013 21:02

 

4aiman
Member
 
Posts: 1208
Joined: Mon Jul 30, 2012 05:47

by 4aiman » Fri Mar 01, 2013 21:25

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.
 

ryguy
New member
 
Posts: 3
Joined: Fri Mar 01, 2013 03:37

by ryguy » Fri Mar 01, 2013 21:38

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.
 

4aiman
Member
 
Posts: 1208
Joined: Mon Jul 30, 2012 05:47

by 4aiman » Fri Mar 01, 2013 21:40

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 ;)
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 9 guests

cron