Multiple States System: Difference between revisions
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). | ||
<syntaxhighlight lang="lua"> | |||
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 | |||
</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). | ||
<syntaxhighlight lang="lua"> | |||
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 | |||
</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.) | ||
<syntaxhighlight lang="lua"> | |||
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 | |||
</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: | ||
<syntaxhighlight lang="lua"> | |||
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!