In ROBLOX I am also learning how to code, but one thing stands out that I feel should be necessary.
What is supposed to happen is that it runs:
function _init() cls() spr(192,20,20,4,4) wait(3) end
But it errors with
"Runtime error line 4 tab 0
Attempt to call global 'wait' (a nil value)
In _init line 4 (tab 0)
At line 1 (tab 1)"
Is there a way to make the program wait a few seconds without taking too much space?
There is no built-in 'wait' command.
Pico basic program pattern is:
- update state & game logic (_update)
- clear + redraw screen (_draw)
To fit into this pattern, wait is nothing more than a counter. Once the counter reaches a given value, move on to the next game state:
local mode="title" local mode_t=0 function _update() mode_t+=1 -- wait 3s (e.g. 90 frames) if mode=="title" and mode_t>3*30 then -- move on to next game state mode="game" mode_t=0 end end function _draw() cls() if mode=="title" then spr(192,20,20,4,4) elseif mode=="game" then -- draw game elseif mode="gameover" then -- draw game over screen end end
Note: many other variants exist - just find one that suits your design.
There's also the question: Why do you want to wait? In a "game loop" pattern, like pico-8 uses, waiting the way you seem to want would take away control from the game loop, which would mean that the entire game would simply hang/freeze for a few seconds. That doesn't sound very useful. Try to think about what you want waiting to actually do, and then try to reframe that in a "game loop" model.
What freds72 meant by "throttling" was just that if you do too much CPU work each frame (ie. each callback of _draw and _update), PICO-8 will "drop frames"; it will skip a _draw() call to catch up. If you're just doing graphics stuff in _draw, then no harm, it will just not be drawn for that frame. (This is what usually happens if a game you're playing "lags" or "stutters" when FPS drops.) However, if you're doing actual game logic in _draw, then your game will behave weirdly and stuff will go out of sync.
If I recall correctly, when Pico-8's CPU is at maximum usage, it will instead call _draw() every other frame to keep up, which can cause problems if you have important stuff in _draw() that needs to happen every frame.
Edit: Whoops! I didn't see Tobias's explanation, so this is kinda redundant.
[Please log in to post a comment]