-
|
I want to implement a simple gamestate-system, similar to the one described here: https://pico-8.fandom.com/wiki/Yet_Another_Multiple_States_System – it uses the "trick" to let a variable hold function names and then call the variable as a function. gamestate=title
function TIC()
gamestate()
end
function title()
cls(2)
print("title gamestate",10,10,12)
if btnp(4) then
init_play()
end
end
function init_play()
gamestate=play
end
function play()
cls(6)
print("play gamestate",10,10,12)
if btnp(4) then
init_title()
end
end
function init_title()
gamestate=title
end... does not work but throws an error What am I doing wrong here? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
Okay, I tested a bit more and found this somehow "dirty" solution – order of function definitions seems to matter. This version, where I define the different gamestate-functions before applying one of them to the variable "gamestate", works! function title()
cls(2)
print("title gamestate",10,10,12)
if btnp(4) then
init_play()
end
end
function play()
cls(6)
print("play gamestate",10,10,12)
if btnp(4) then
init_title()
end
end
gamestate=title
function TIC()
gamestate()
end
function init_play()
gamestate=play
end
function init_title()
gamestate=title
endBut still this seems to be a bit awkward ... isn't there a better way to write the line |
Beta Was this translation helpful? Give feedback.
-
|
Use the BOOT function for startup code. https://github.com/nesbox/TIC-80/wiki/BOOT |
Beta Was this translation helpful? Give feedback.
-
|
I found out a solution that seems to be the best / most logical for me: function BOOT()
gamestate=title
end
function TIC()
gamestate()
end
function init_play()
gamestate=play
end
function init_title()
gamestate=title
end
function title()
cls(2)
print("title gamestate",10,10,12)
if btnp(4) then
init_play()
end
end
function play()
cls(6)
print("play gamestate",10,10,12)
if btnp(4) then
init_title()
end
end |
Beta Was this translation helpful? Give feedback.
I found out a solution that seems to be the best / most logical for me:
The built-in BOOT-function seems to be called as the first function but after TIC-80 has "had a look at" the rest of the code.
Thus putting
gamestate=titleinside BOOT also works and I can happily put the function "title" wherever I want.My solution: