Page 1 of 1

Input with Jump Buffer

Posted: Thu Apr 18, 2024 11:47 pm
by glass2d
I've implemented a jump buffer feature in my game, but I've encountered an issue: when I hold down the "d" key to move right, any jump triggered within the jump buffer window doesn't register until I release the "d" key. I'm seeking guidance on how to address this behavior and aiming to gain a deeper understanding of why it's occurring.

-- main.lua
...
function love.keypressed(keypressed)
Player:jump(keypressed, nil)
end
---

-- player.luq
...
function Player:jump(keypressed, keyreleased)
if keypressed == "space" then
self.jumpBufferTimer = self.jumpBufferTime
end
if (self.coyoteTimer > 0 and self.jumpBufferTimer > 0) then
self.yVel = self.jumpAmount
self.grounded = false
self.coyoteTimer = 0 -- Prevents player from multi-jumping by spamming the jump button
self.jumpBufferTimer = 0
end
if keyreleased == "space" then -- Allows the player to jump less high when the jump button is pressed quickly
self.yVel = self.yVel * 0.5
end
end
...

Re: Input with Jump Buffer

Posted: Fri Apr 19, 2024 2:20 am
by MrFariator
love.keypressed and love.keyreleased events only trigger once for a given key press and release. Think of them as alarms for when either action happens (press, release), as opposed to them getting continuously checked. The way your code is laid out, Player:jump is called only when these events are triggered, hence why you get the jump when releasing the key. If you want to implement a jump buffer, you'd have to implement the behavior in a way that it continuously checks for the conditions (is the jump key held down, and was it pressed within the jump buffer's allowance?), as opposed to only checking when the events happen.

This could be done by putting the "status" of a key to a table based on the press/release events; effectively you keep a list of keys that have been pressed, and for how long. You could also optionally keep track of when a given key was released. Then, once you've done that, you make your player object check this table whenever you need to, like for this jump buffer situation.

Re: Input with Jump Buffer

Posted: Fri Apr 19, 2024 3:14 pm
by dusoft
Try using this instead in your love.update() function:

Code: Select all

if love.keyboard.isDown('up') then
...
end
[code]

Re: Input with Jump Buffer

Posted: Fri Apr 19, 2024 8:15 pm
by MrFariator
The issue with love.keyboard.isDown is that if your framerate is low, or the game happens to lag, your key strokes may not get recognized when the update functions run (effectively you tap the button in between the update ticks, making love.keyboard.isDown unable to tell what you did). On that front, I prefer using love.keypressed/released for any non-continuous inputs (like tapping the jump button). And once you set up the system with those event callbacks, it's not too hard to just rely on them.

Re: Input with Jump Buffer

Posted: Sat Apr 20, 2024 1:14 pm
by dusoft
That might be, but if we are talking post 2012+ laptops that should not happen, if other e.g. graphics intensive operations are not taking place.

Re: Input with Jump Buffer

Posted: Sat Apr 20, 2024 3:09 pm
by pgimeno
If you're losing keypresses because of low frame rate, you can't time them either, so in this case I don't think it makes a difference.

Re: Input with Jump Buffer

Posted: Sun Apr 21, 2024 9:19 pm
by glass2d
Thanks all for the input. I was able to refactor and use a more modular input system which resolved the issue