Log In  

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

P#73239 2020-02-19 18:31

1

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.

P#73248 2020-02-19 18:51

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.

P#73249 2020-02-19 18:53

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

P#73251 2020-02-19 18:55

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!

P#73253 2020-02-19 19:12 ( Edited 2020-02-19 19:13)
1

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
P#73255 2020-02-19 19:35 ( Edited 2020-02-19 19:36)

Thanks a lot! :))

P#73256 2020-02-19 19:41

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

P#73307 2020-02-20 22:57

poke(0x5f5c,255) fixes this now :)

P#136273 2023-10-23 15:27

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 10:15:20 | 0.008s | Q:20