HUMP - Pass in a value on enter or init

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
pikuchan
Prole
Posts: 16
Joined: Wed Jan 09, 2013 1:01 am

HUMP - Pass in a value on enter or init

Post by pikuchan »

I am using HUMP to manage my gamestates but am having trouble getting it to let me pass in a variable value. Here is some code to help.

This is in my main game.lua file:

Code: Select all

	--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.

Code: Select all

function gameOver:init(message)	
	stringToDisplay = message
end

function gameOver:enter(message) 
    stringToDisplay = message
end
User avatar
IndieKid
Citizen
Posts: 80
Joined: Sat Dec 22, 2012 7:05 pm
Contact:

Re: HUMP - Pass in a value on enter or init

Post by IndieKid »

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:

Code: Select all

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
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - Pass in a value on enter or init

Post by vrld »

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:

Code: Select all

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.

hump | HC | SUIT | moonshine
pikuchan
Prole
Posts: 16
Joined: Wed Jan 09, 2013 1:01 am

Re: HUMP - Pass in a value on enter or init

Post by pikuchan »

vrld wrote:
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:

Code: Select all

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.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: HUMP - Pass in a value on enter or init

Post by vrld »

pikuchan wrote:I completely understand this.
Are you sure? Can you answer these questions?
  • When is gameOver:init() called?
  • What arguments, if any, are passed to gameOver:init() when calling Gamestate.switch(gameOver, message)?
  • When is gameOver:enter() called?
  • What arguments, if any, are passed to gameOver:enter() when calling Gamestate.switch(gameOver, message)?
  • When are you checking for nil-ness of your variable?
If your problem persist, please upload a .love showing the problematic behavior.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
pikuchan
Prole
Posts: 16
Joined: Wed Jan 09, 2013 1:01 am

Re: HUMP - Pass in a value on enter or init

Post by pikuchan »

I figured it out, basically, i'm just too used to object oriented programming still :)

I was expecting it to do something like

gamestate.switch(newGamestate, varsToPass)
then accept
gamestate:enter(varsToPass)

Obviously, it actually needs to be something like:
gamestate:enter(gamestate, varsToPass)

Amazing how long I scratched my head on that one!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 227 guests