--update our player, but only if they are alive
if player.alive == true then
playerUpdate(dt)
else
message = "Cannon Destroyed"
Gamestate.switch(gameOver, message)
end
My understanding from the HUMP documentation is that message will be passed in to the gameover:init function, possibly also the enter, and use that value. But when I use the code below, I get a value of nil. Any suggestions? I have verified that the init is getting hit, message just does not have a value.
Maybe that's because of local for message. Game thinks that it's made for each function separately. I'm not sure. But I think it's more easy(in case if you having trouble) to do like I did:
function love.load()
gameState = "intro"
end
function love.update()
if gameState == "game" then -- checks if we should update level
...updateGame...
...doSomething...
elseif gameState == "pause" then
...doSomething(no game value updates, so all will freeze)...
elseif gameState == "menu" then
...doSomething...
end
function love.keypressed(key)
if gameState == "game" then
...controls...
elseif gameState == "pause" then
...pauseControls...
elseif gameState == "menu" then
if menuSelection == 1 and key == "return" then -- menu selection 1 is new game for example
gameState = "game" -- how to change game state
end
...menuControls...
end
end
function love.draw() -- you draw "level"(Player, Bot, etc.) in both gameStates "game" and "pause"
if gameState == "game" then -- your "level" will update(because of love.update())
drawPlayer()
drawBot()
drawObjects()
drawHUD()
...someThingElse...
elseif gameState == "pause" then -- your "level" won't update(because of love.update())
drawPlayer()
drawBot()
drawObjects()
drawPause()
...someThingElse...
elseif gameState == "menu" then
drawMenu()
...someThingElse...
end
end
pikuchan wrote:My understanding from the HUMP documentation is that message will be passed in to the gameover:init function, possibly also the enter, and use that value.
state:init() is only called once (the first time you switch to the state) and does not retrieve any arguments. state:enter(previous, ...) retrieves the state you are switching from (previous) and any other parameters to pass to switch(). The code does practically this:
function GS.switch(to, ...)
current_state:leave()
local old_state = current_state
current_state = to
if not is_initialized(current_state) then current_state:init() end
return current_state:enter(previous, ...)
end
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.
pikuchan wrote:My understanding from the HUMP documentation is that message will be passed in to the gameover:init function, possibly also the enter, and use that value.
state:init() is only called once (the first time you switch to the state) and does not retrieve any arguments. state:enter(previous, ...) retrieves the state you are switching from (previous) and any other parameters to pass to switch(). The code does practically this:
function GS.switch(to, ...)
current_state:leave()
local old_state = current_state
current_state = to
if not is_initialized(current_state) then current_state:init() end
return current_state:enter(previous, ...)
end
I completely understand this. I still don't understand why my variable "message" is nil when the enter method is called. I'm not having any trouble with the state management and switching states. I am getting through 3 states successfully before hitting this point. I can print debug code that shows me that message has a value when I pass it in to my gamestate.switch method, but it is gone when the enter method is called of the gameover state.