Input with Jump Buffer

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
glass2d
Prole
Posts: 14
Joined: Thu Apr 18, 2024 11:40 pm

Input with Jump Buffer

Post 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
...
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: Input with Jump Buffer

Post 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.
User avatar
dusoft
Party member
Posts: 634
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Input with Jump Buffer

Post by dusoft »

Try using this instead in your love.update() function:

Code: Select all

if love.keyboard.isDown('up') then
...
end
[code]
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

Re: Input with Jump Buffer

Post 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.
User avatar
dusoft
Party member
Posts: 634
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Input with Jump Buffer

Post 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.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Input with Jump Buffer

Post 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.
glass2d
Prole
Posts: 14
Joined: Thu Apr 18, 2024 11:40 pm

Re: Input with Jump Buffer

Post by glass2d »

Thanks all for the input. I was able to refactor and use a more modular input system which resolved the issue
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 6 guests