Log In  

Hello everyone!
I bought PICO-8 some time ago but only recently decided to actually learn it.

One (of many) thing that got me a little confused is the order and speed execution.

The code below shows the text on screen:

function _update()
    cls()
    print("hello",64,64,11)
end

However, if I change the code as bellow, the text does not show anymore:

function _update()
    print("hello",64,64,11)
    cls()
end

What I got from this is: on the second code, since the last command being processed is the cls(), then the screen ends up 'blank'.

But what makes me confused is that I thought since pico-8 runs on an endless loop the cls() and print() will always be called one after another, so I don't understand why in one order the print() is the 'final' and in the other, the cls() is.

Can anyone help me understand this better?
I couldn't find anything that explained this behavior (maube because it's too basic, but since I'm new to this it doesn't make sense to me).

P#144148 2024-03-22 18:16

_update() does the core work of your game behind-the-scenes, while _draw() sets the stage for every frame (often using data that was calculated in your update loop). The next frame displayed will be the final result of your draw operations performed in order. So, clearing the screen and then printing text will print that text on a blank background, while printing text and then clearing the screen will show a clear screen.

It's worth noting that PICO-8 will always try to run code in _update() at the target framerate, while it will skip _draw() if CPU throttled, so this is why our core logic should stay in _update() and draw operations (and related logic that may be skipped when frameskipping) kept in _draw().

P#144152 2024-03-22 18:37

you are correct that pico-8 runs _update and _draw in a loop, what is missing is that there is a little pause after each call (shorter or longer depending on the time spent in _update and _draw ‑ pico8 has a fixed timing, 30 or 60 frames per second). so your code is printing then clearing the screen, and that’s the result of these operations that’s visible during the little pause.

these articles go into depth on the why and how of this technique:
https://gameprogrammingpatterns.com/double-buffer.html
https://gameprogrammingpatterns.com/game-loop.html
(they make me appreciate what pico8 does for us automatically!)

P#144185 2024-03-23 01:26 ( Edited 2024-03-23 01:27)

[Please log in to post a comment]