I'm a new Pico-8 user. I feel like this should have some crazy obvious answer right in front of my nose.
This function works fine if called in _draw(), but I cannot get a btnp to call it (or any other function) -
function test_swing() print("swing", 100, 100) end
The btnp code in _update() is
if btnp(4) then test_swing() end
I would expect this to just flash up briefly at the 100,100 coordinate on the button press (z). Calling it
in _draw() prints it to the screen continuously as expected. Any ideas? Thanks
are you by chance calling cls at the top of your _draw function? If so, stuff you draw from _update is getting erased by the time the screen buffer is flipped at the end of _draw
The other thing to note is that in general it's a good idea to separate state-changing code from drawing code; the update/draw callback structure is conducive to that. In other words, your draw function should be able to be called multiple times in a row and still draw the same thing each time, because no state is changing. The other side of this is that the update function should not draw anything.
So in this case, what I would do is set a variable when a button is pushed, and then write separate drawing code which prints something based on the value of that variable.
It looks like you may be printing stuff for temporary debug purposes though, in which case that rule doesn't necessarily need to be followed :)
EDIT: ha, it looks like we commented at like the same time, so you haven't seen this yet, and are replying not to this post, but to the previous one? well, whenever you see this, hopefully it will help :)
I think it's a bad idea using "btnp" inside the "_draw"-function, because the Button-Press is Frame-Dependend if using in that way. If you press the Button between Frames, it's possible that Pico-8 doesn't recognised the key, which feels like Pico-8 has "eaten" the Key.
Normally, you would use a Variable to "save" the last-pressed key in a Variable, and in _draw you would check that Variable. For instance:
function _init() pressed_4 = false end function test_swing() if pressed_4 then print("swing",100,100) end end function _update() if btnp(4) then pressed_4 = true end if btnp(5) then pressed_4 = false end end function _draw() cls() test_swing() end
Yes, this is similar to what I already said. Eusebia32 hasn't actually seen my reply yet though; it seems their reply came at almost exactly the same time I posted mine, and they were responding to the post before mine :D
@kittenm4ster - thanks. Right, cls was being called at the top. Thanks for your advice. I'm still getting used to the subtleties of game looping. I definitely understand what you're saying. I did what you suggested and was able to get it to print in _draw using a true/false toggle triggered by the btnp in _update.
[Please log in to post a comment]