In your case, I would go one step further and just load the states dynamically:
Code: Select all
local loaded = {}
local current_state = nil
function gamestates.set_state( state_name, ... )
-- exit the old state
gamestates.state_event( 'exit' )
-- check if loaded
current_state = loaded[state_name]
if not current_state then
-- load the new state
current_state = require(state_name)
gamestates.loaded[state_name] = current_state
gamestates.state_event( 'load', ... )
end
-- enter new state
gamestates.state_event( 'enter', ... )
end
I think these could be "hidden" from the rest of the code - allowing accessing only through the use of API that we already have:
gamestates.set_state("menu", ...)
gamestates.state_event("draw", ...)
You could probably add more functionality if needed:
gamestates.get_previous() -- returning a string
This is just my personal opinion though, the code is fine as it is, so great work there!