Btnp() returns true even when button continuously pressed down
Quick Recap
Essentially when I use btnp() for jumping, when keeping the button pressed down my character jumps, then pauses for a variable amount of time, then jumps again, ...
Thanks for your help :)
The code:
This code is in a function (p1()) that I call in _update60
--/jump\-- if p1.grounded then p1.jumptemp=0 end if btnp(⬆️) and p1.jumptemp<2 then p1.dy=0 p1.dy-=p1.jpp p1.jumping=true p1.jumptemp+=1 end |
Simplified code:
--/jump\-- if btnp(⬆️) and p1.jumptemp<2 then p1.dy=0 p1.dy-=p1.jpp end |
Detailed problem and attempts at troubleshooting
Hi everyone, so I'm quite new to pico-8, making my second game which is a platformer. To jump I used BTNP(up) which I believe is meant to: "return true only if a given button was pressed just now, and does not report true again for the same button in the next frame even if it is held down"(pico 8 fandom).
But for some reason, when I tried it in my game, when I hold the button down it jumps, waits for a variable amount of time, and jumps again (and so on but I have a double jump limit). I'm not sure why I already figured my key might be a bit dis-functioning but I used a keyboard checker and it seems to be ok. So I'm thinking it has to be something in my code, if you guys know what's wrong please help me :)



This is normal, expected behavior. If you keep holding down the button, btnp() will register again 15 frames later, and then if it's kept held down, it will keep registering in 4 frame increments until it is let up again.



It is possible to write your own function using only btn() that only registers a single time on pressing a button down, and then will not register again until the button is lifted and pressed down again.



Thanks a lot, really helpful! :)
might sound stupid but how should I code this function?



Basically, you would have variables that track the pressed and newly_pressed states of a button using btn(). For example, pressed and newly_pressed would both start as false. Then, every frame you'd check btn(❎) to see if it comes back as true.
If btn(❎) comes back as true, then...
You'd then only set newly_pressed to true if and only if pressed was false. Because that means ❎ wasn't pressed in the last frame.
But if pressed was already true, that means ❎ was already being pressed last frame, so newly_pressed would get set back to false.
Finally, after setting newly_pressed to either true or false, you'd set pressed to true.
But if btn(❎) comes back as false, then...
You'd set pressed and newly_pressed to false.
Hope that helps!



Here's a chunk of code you can paste into a new cart to test. The functions you actually need are the init_true_btnp(), btn_states(), and true_btnp(). (And of course you have to put init_btn_states() into your _init() and btn_states() at the top of your _update(). Then you can use true_btnp() where you need it.) Also, please note this is just sample code. This does NOT fully replace btnp() in all its functionality. For instance, this has no multiplayer support or the other methods of using btnp(). This is just basic code that works that you could build off of.
function _init() init_true_btnp() --must be in _init! cls() frame=0 end function _update() btn_states() --must be first in _update! if (true_btnp(❎)) print(frame) frame+=1 end function init_true_btnp() btn_new={} btn_press={} for i=0,5 do btn_new[i]=false btn_press[i]=false end end function btn_states() for i=0,5 do if (btn(i)) then btn_new[i]=not btn_press[i] btn_press[i]=true else btn_new[i]=false btn_press[i]=false end end end function true_btnp(key) return btn_new[key] end |



FYI it has been announced on twitter that there will be a new feature in the next release (hopefully real soon now?) which will add an address you can poke to disable the repeat behavior entirely :)
[Please log in to post a comment]