Log In  

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

P#52485 2018-05-07 06:26 ( Edited 2018-05-07 19:40)

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.

P#52486 2018-05-07 06:35 ( Edited 2018-05-07 10:35)

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

P#52487 2018-05-07 06:42 ( Edited 2018-05-07 10:42)

Thanks freds72 for your input.

P#52488 2018-05-07 06:44 ( Edited 2018-05-07 10:44)

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

P#52489 2018-05-07 07:19 ( Edited 2018-05-07 11:19)

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

P#52490 2018-05-07 07:20 ( Edited 2018-05-07 11:20)

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

P#52491 2018-05-07 07:21 ( Edited 2018-05-07 11:21)

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:

P#52492 2018-05-07 07:41 ( Edited 2018-05-07 13:08)

Thanks for the link tobiasvl,

You're right, my understanding is rubbish :(

P#52494 2018-05-07 09:22 ( Edited 2018-05-07 13:22)

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.

P#52496 2018-05-07 10:23 ( Edited 2018-05-07 14:29)

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.

P#52497 2018-05-07 10:32 ( Edited 2018-05-07 14:36)

Thanks everyone for your help.

P#52507 2018-05-07 15:40 ( Edited 2018-05-07 19:41)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-19 20:31:46 | 0.011s | Q:27