Multiple States System: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
imported>Felice Enellen
(add syntax highlighting)
 
Line 13: Line 13:
The function "_init()" is what happens when the game is started up (or in this case, the State).
The function "_init()" is what happens when the game is started up (or in this case, the State).


function _init()
<syntaxhighlight lang="lua">
  titleinit() -- does title things.
function _init()
end
  titleinit() -- does title things.
end
function titleinit()
 
  mode = 0
function titleinit()
  --whatever you wish to do when the title starts up.
  mode = 0
end
  --whatever you wish to do when the title starts up.
end
function gameinit()
 
  mode = 1
function gameinit()
  --what to do at the start of the play session.
  mode = 1
end
  --what to do at the start of the play session.
end
</syntaxhighlight>


We'll call on gameinit() later.
We'll call on gameinit() later.
Line 32: Line 34:
The function "_update()" is where gameplay elements are changed (like a timer).
The function "_update()" is where gameplay elements are changed (like a timer).


function _update()
<syntaxhighlight lang="lua">
  if (mode == 0) then --Title Screen mode
function _update()
    titleupdate()
  if (mode == 0) then --Title Screen mode
  else
    titleupdate()
    gameupdate()
  else
  end
    gameupdate()
end
  end
end
function titleupdate()
 
  --Example "Press Z to start"
function titleupdate()
  --if btn(4,0) then
  --Example "Press Z to start"
  --  gameinit()
  --if btn(4,0) then
  --end
  --  gameinit()
end
  --end
end
function gameupdate()
 
  --gameplay elements (or whatever used to be in _update) goes here.
function gameupdate()
end
  --gameplay elements (or whatever used to be in _update) goes here.
end
</syntaxhighlight>


The example given can be used by simply getting rid of the dashes (or rather uncommenting it).
The example given can be used by simply getting rid of the dashes (or rather uncommenting it).
Line 56: Line 60:
The function "_draw()" is called to draw things to the screen. (There's some repetition in this one.)
The function "_draw()" is called to draw things to the screen. (There's some repetition in this one.)


function _draw()
<syntaxhighlight lang="lua">
  if (mode == 0) then --Tit
function _draw()
    titledraw()
  if (mode == 0) then --Tit
  else
    titledraw()
    gamedraw()
  else
  end
    gamedraw()
end
  end
end
function titledraw()
 
  --Example "Title"
function titledraw()
  --print("Super Cool Game")
  --Example "Title"
end
  --print("Super Cool Game")
end
function gamedraw()
 
  --the screen drawing mechanisms(or whatever used to be in _draw) goes here.
function gamedraw()
end
  --the screen drawing mechanisms(or whatever used to be in _draw) goes here.
end
</syntaxhighlight>


== Notes ==
== Notes ==
- If you want to add more to "_draw" or "_update", simply follow this guideline:
- If you want to add more to "_draw" or "_update", simply follow this guideline:
else if (mode == 2) then
<syntaxhighlight lang="lua">
    --optiondraw() or optionupdate()
else if (mode == 2) then
    --optiondraw() or optionupdate()
</syntaxhighlight>
just be sure to put that before the else statement!
just be sure to put that before the else statement!
[[Category:Tutorials]]
[[Category:Tutorials]]

Latest revision as of 04:53, 13 February 2018

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!