Hi Community,
I new to pico8 (loving it) and beginner to programming - especially LUA.
So far, I've got a player moving in 4 directions (whoots) on TAB 00 and I'd like a menu function on TAB1.
I haven't quite got my head around the menu-screen yet (after making a right dog's dinner when trying to follow Light Bikesdescription
Would you suggest I create a function on TAB1 and callit it from _INIT on tab00?
I've searched online for a simple solution, but failed to find/understand it :(
Sorry if this seems basic - kinda like me really!
Thanks,
Roberto



Tabs are just a convenient way of organizing code - it is the same as putting code in the tab order in tab 0.
Calling a global function from _init will work, regardless of the tab/code location.



ok, managed to work out calling the function DO_function_name from INIT on Tab00



Arrrght, I'm trying to understand this but failing!!!!!
TAB00
val = 0
function _init()
cls()
if val ==0 then
do_intro()
else
do_draw()
end
end
function _update()
if (btn(4)) then _draw() end
end
function _draw()
print("go")
end
TAB01 >
function do_intro()
print("press z to start")
end



so I want the game to start with CLS and message "press Z", if key Z is pressed, the CLS and print "GO"



I put var 'val' in as it would be 0 - right, the call function _intro



I think you're misunderstanding how the game loop works. In _update(), you call _draw() if a button is pressed – but _draw() is called automatically each frame.
You also do some checking in _init(), but _init() is only called once when the game is initialized. Your test doesn't do anything. unless you call _init() again. Is do_draw() even declared anywhere?
I think you should read these articles:



Thanks for the link tobiasvl,
You're right, my understanding is rubbish :(



Here, I edited your code to do what it seems you want it to do. I hope it helps. I didn't utilize a proper multiple state system, I just tried to fix up your existing structure by moving the logic. Hopefully that makes it easier for you to compare it with your code to see what you did wrong.
--tab00 function _init() val = 0 end function _update() if (btn(4)) then val=1 end end function _draw() cls() if val==0 then do_intro() elseif val==1 then print("go") end end --tab01 > function do_intro() print("press z to start") end |
I didn't understand what do_draw() was supposed to do, so I didn't do anything with it.
Basically, _init() initializes the state, _update() changes the state (which is the val variable in your code, which you never changed originally), and then _draw() draws the screen based on what the state is. That's the general gist of it.



In general, declaration order within a tab or across tabs shouldn't matter as long as you only do initialization of global variables inside of _init() and you only call your functions from inside of _init(), _update(), or _draw(). That way all of your source code is scanned/declared before any code is actually executed.
The way PICO-8 works is that it adds a bit of code at the end of the visible code you write. This is a very simplified version of it:
-- (your code would be up here) -- begin invisible appended code: if(_init) _init() if _update or _draw then while true do if(_update) _update() if(_draw) _draw() flip() end end -- end invisible appended code |
This means all of your declarations get read before _init()/_update()/_draw() are called, allowing you to reference anything, regardless of the order they were declared.
[Please log in to post a comment]