This is a very simple state switcher for Löve games (or any other Lua program).
The short answer: it depends.
The long answer:
- Very simple: one state - one file
- Re-usable functions - functions declared in previous states can be re-used
- Modifyable on the fly - simply modify and reload a state file
- Easy to understand and modify - for example 'local' version or ability to read out current state is 1-2 LOC
- Re-usable functions - functions declared in previous states must be re-declared if you do not want them to they stick around. This could lead to weird bugs if you are not careful. If you want to have clean game states by default I can recommend hump.gamestate.
- Argument conventions - you have to create and stick to your own conventions for passing values
See the included example (simply copy stateswitcher.lua into the example folder to make it work) or have a look at this brief explanation.
You need to require the library once before using it.
-- in main.lua
require('stateswitcher')
-- switch to some gamestate
gamestate.switch('myGameState.lua')
Any lua file can be a state, but I recommend to cleanly separate them from non-state files.
-- in myGameState.lua
-- switch to some other state and pass arguments
gamestate.switch('anotherGameState.lua', {2, somekey='somestring', anotherkey=3.41, sometable={yes='no'}})
Read passed values from another game state and clear the arguments.
-- in anotherGameState.lua
-- read arguments either by index or key
local double = gamestate.args[1]
local s = gamestate.args.somekey
local pi = gamestate.args.anotherkey
local t = gamestate.args.sometable
print(double)
print(s)
print(pi)
print(t.yes)
-- clear the arguments table
gamestate.args = {}
This code is based on the idea by Daniel Duris and an improved version of Fredrik Vestin and licensed under the CC BY 4.0. The original and improved versions can be found on ambience.sk.
- The original code weighted in at 22 LOC. Fredrik reduced it to just 12 and made it more flexible in the process. I tried to do the same and reduced it to just 5 LOC.
- The library declares a single global variable and is intended to be used accordingly. Simply require it once:
require('stateswitcher')
. It is trivial to convert it to a 'localized' version (at the cost of 1 LOC), but I do not see any benefit in doing this for this library. - I replaced
require()
withdofile()
. This way I can avoid thepackage.loaded()
-hack. Why cache a file in the first place if you then remove the file from the cache? The drawback is that you have to use the filename instead of the module name, but I guess that just reinforces the 'one file per state'-idea. - Fredrik introduced the possibility to pass tables to states by using Luas the three dot parameter (
...
) instead of a string. I went a tiny step further and simply pass a table. This has one clear benefit: I can use any key I want instead of just indices. I could also remove theclear()
-function and simply assing an empty table instead. - I adapted the examples slightly to reflect how my version works.