Hi everyone!
Currently i'm trying to make a space shooter but i got stuck;
I have a "game" that manages the main game and i wanted the game.enter() function to load the map only if the previous gamestate was "gameover", but i'm having problems passing the old state name, it keep saying that i'm passing a nil value.
I've tried
Code: Select all
function game.enter(...)
local prev = ...
if prev == "gameover" then
map_manager.load("map.space1")
end
I'm using noooway's gamestates module:
Code: Select all
local gamestates = {}
local current_state = nil
local loaded = {}
function gamestates.state_event(function_name, ...)
if current_state and type(current_state[function_name]) == "function" then
current_state[function_name](...) -- sends additional args to game.enter()
end
end
function gamestates.set_state(state_name, ...)
gamestates.state_event("exit")
local old_state_name = get_key_for_value(loaded, current_state) -- get_key_for_value(table, value) return key
current_state = loaded[ state_name ]
if not current_state then
current_state = require( "gamestates/" .. state_name )
loaded[ state_name ] = current_state
gamestates.state_event("load", old_state_name, ... )
end
gamestates.state_event("enter", old_state_name, ... )
end
function get_key_for_value(table, value)
for k,v in ipairs(table) do
if v == value then
return k
end
end
end
return gamestates