Multiple States System

From Pico-8 Wiki
Jump to navigation Jump to search

Description

Ever wanted a Title Screen or a Menu?

This guide can show you how to manage multiple states at the same time!

Note: For examplatory purposes, we'll be using the prefixes "game" and "title" for this tutorial.

Instructions

Initializing

The function "_init()" is what happens when the game is started up (or in this case, the State).

function _init()
  titleinit() -- does title things.
end

function titleinit()
  mode = 0
  --whatever you wish to do when the title starts up.
end

function gameinit()
  mode = 1
  --what to do at the start of the play session.
end

We'll call on gameinit() later.

Updating

The function "_update()" is where gameplay elements are changed (like a timer).

function _update()
  if (mode == 0) then --Title Screen mode
    titleupdate()
  else
    gameupdate()
  end
end

function titleupdate()
  --Example "Press Z to start"
  --if btn(4,0) then
  --  gameinit()
  --end
end

function gameupdate()
  --gameplay elements (or whatever used to be in _update) goes here.
end

The example given can be used by simply getting rid of the dashes (or rather uncommenting it).

Drawing

The function "_draw()" is called to draw things to the screen. (There's some repetition in this one.)

function _draw()
  if (mode == 0) then --Tit
    titledraw()
  else
    gamedraw()
  end
end

function titledraw()
  --Example "Title"
  --print("Super Cool Game")
end

function gamedraw()
  --the screen drawing mechanisms(or whatever used to be in _draw) goes here.
end

Notes

- If you want to add more to "_draw" or "_update", simply follow this guideline:

else if (mode == 2) then
    --optiondraw() or optionupdate()

just be sure to put that before the else statement!