Log In  

Hi there!

New PICO-8 programmer and relatively new programmer in general.

I'm making a very simple "pong" game just to get the hang of PICO-8 and Lua, but I can't figure out how to start a game paused. I considered setting a variable "paused" and flip it between 0 and 1 using keyboard input (and then running the update functions when paused = 0), but then it would have to be the Z or X key which I might use later on for something else.

How does everyone else handle this? Or can you point me to a web-resource that explains the best way to handle this?

Thanks for putting up with newbie questions!

Sincerely,
MorsdenMan

P#98264 2021-10-06 02:08

Hello and welcome!

Most games don’t really start paused (meaning game suspended with the pico8 pause menu open), rather they have a title screen and require a user action to transition into main game screen.

The concept you need is: game states!
Have a global variable to record if the game is in title or main (could be a boolean true/false or a string used with == comparisons). In _update, if we are in title AND button is pressed, then change state to be main. If we are in main AND button is pressed, then something else happens.

Does that make sense?

(There are more advanced ways for this to handle more complicated states and transitions, but it’s best to start simple!)

P#98268 2021-10-06 03:20

So here's how I handle pausing. I have a bool I usually call "paused", which in your case would be set to "true" at the start. Then, inside my update function, I'll put all the things I don't want to update inside a conditional like:

paused = true

function _update()
  if not paused then
    --put everything that I want to not update while paused
  end

  --outside that conditional, I put everything that should update even while paused like UI, music, etc.

  --then, to flip paused/unpaused...
  if (btnp(X)) paused = not paused
end

You can of course put whatever button you want to flip the boolean.

Hope this helps.

P#98292 2021-10-06 18:33 ( Edited 2021-10-06 18:45)

Merwok -- Thank you so much for the explanation. I think I understand that. Actually, I think I have been doing a hacked version of that, but I see notes I should be E to do a cleaner jobs of coding it. Thanks!

UnitVector -- Thanks! Sorry I didn't get back sooner, I thought I had subscribed to replies, but I didn't, so I didn't know you had back. Sorry! I ended up doing something like you suggested (I got the idea from a RayLib tutorial). But I had trouble with the Button being read a ton of times a second flipping paused from true to false like a dozen times in one press! I ended up using X for pause and Z for play, but I'm still not sure of my error... any thoughts?

P#98324 2021-10-07 12:15

Wait... is btn(X) different from btnp(X) ?

P#98325 2021-10-07 12:16

Yes. btn(X) reads it every frame, but btnp(X) just reads it the first time it's pressed, as long as you don't keep holding it down. Use btnp().

P#98327 2021-10-07 13:06

Note that btnp will repeat after some time if the button is kept pressed; this can be configured: https://www.lexaloffle.com/dl/docs/pico-8_manual.html#BTNP

P#98330 2021-10-07 14:09

Thank you, both! I understand better now. I'll give that a try and make a note of it. Then I'll have to try another attempt at game states.

Thank you very much!

UPDATE -- It worked perfectly! Thanks again!

P#98367 2021-10-08 01:23 ( Edited 2021-10-08 03:17)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 23:18:14 | 0.007s | Q:20