Log In  

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

P#58798 2018-11-05 12:59 ( Edited 2018-11-05 19:27)

Could do it this way:

function _init()
end

function _draw()
  cls()
  if btnp(4) then
    test_swing()
  end
end

function _update()
end

function test_swing()
  print("swing",100,100)
end

This works.

P#58799 2018-11-05 13:41 ( Edited 2018-11-05 18:41)

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

P#58800 2018-11-05 14:08 ( Edited 2018-11-05 19:18)

Right - thanks. Since it does work on a btnp in _draw, the issue must be something on btnp in _update. I'm just not sure what the problem is with using it - or any other function - on a btnp in _update.

P#58801 2018-11-05 14:08 ( Edited 2018-11-05 19:08)

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
P#58802 2018-11-05 14:10 ( Edited 2018-11-05 19:10)

@Astorek86
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

P#58803 2018-11-05 14:20 ( Edited 2018-11-05 19:20)

@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.

P#58804 2018-11-05 14:23 ( Edited 2018-11-05 19:23)

@Astorek86 - yes, thank you. Yours was similar to what I did after seeing kittenm4ster's response. It's nice to be on a helpful forum for once :).

P#58805 2018-11-05 14:27 ( Edited 2018-11-05 19:27)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 12:13:25 | 0.032s | Q:22