Page 1 of 1

Some clarification on HUMP.gamestate needed [Solved]

Posted: Wed Jul 30, 2014 6:12 pm
by fedexist
So, i started using hump.gamestate to give my prototype a structure, but i'd like to have some clarification about the using of it (yes i've read the documentation on the github page) inside my code.

I want to have, for now, 2 states: menu and world_map. Each one with its own lua file. In my main.lua i've got something like this:

Code: Select all

module(..., package.seeall)
require("entities")
require("menu")
require("world_map")

gamestate = require "hump.gamestate"

function love.load()

	gamestate.registerEvents()
	gamestate.switch(menu)
	
end

function love.update(dt)
	
	entities:update(dt)

end

function love.keyreleased(key)	
	if key == "escape" then
      love.event.quit()
   end

end

function love.draw()
	

end

function love.quit()
  print("Thanks for playing!")
end
Now, with what's written now, when I call gamestate.switch(menu) i go to initialisation of the menu and then I enter its state, where the game updates what needs to be updated (if its present the menu:update(dt) function) and draw what needs to be drawn (menu:draw(), both these functions are called thanks to gamestate.registerEvents(), which looks for callbacks and execute them). For now, everything is fine menu is displayed and if i run this line: if gamestate.current() == menu then print("I'm in the menu") end it actually prints.

Now, I want to switch state and would like to go the world_map when i press enter and for this purpose I write this:

Code: Select all

function menu:keyreleased(key)
    if key == 'enter' then
		print("I pressed enter")
        gamestate.switch(world_map)
    end
end
Now, shouldn't this bring me to the world_map state? For the same reason as before gamestate.registerEvent() will call menu:keyreleased(key) whenever I get an input. And this is my problem: i'm stuck in the menu state and doesn't recognize Enter as the proper input.

Can anyone give me an advice/example on how I should use properly HUMP.gamestate and what I'm missing in my current code (yes, they are just some snippets, but i thought it would be enough for my issue, i can give a .love if needed)?

Re: Some clarification on HUMP.gamestate needed

Posted: Wed Jul 30, 2014 8:48 pm
by Kingdaro
The enter key is actually "return". https://www.love2d.org/wiki/KeyConstant

Re: Some clarification on HUMP.gamestate needed

Posted: Wed Jul 30, 2014 9:16 pm
by fedexist
Oh. That's it? Well, damn, that was very stupid on my part. Thanks for the reply :)

Re: Some clarification on HUMP.gamestate needed

Posted: Thu Jul 31, 2014 9:34 am
by fedexist
Ok, sorry for double posting, but I can't get it to work. When I press enter, I have an error, since in menu.lua there's a call to the gamestate variable and I get this "Attempt to index global variable 'gamestate' (a nil value)", due to the variable scope I guess. Furthermore, if i change in the main.lua the first state with world_map in place of menu, I simply don't access it, and can't understand why.

Code for reference:

menu.lua

Code: Select all

menu = {}

function menu:draw()
    love.graphics.print("Press Enter to continue", 10, 10)
end

function menu:enter(previous)
	
end

function menu:keyreleased(key)
    if key == 'return' then
		print("Ho premuto invio")
        gamestate.switch(world_map)
    end
end
world_map.lua

Code: Select all

local world = require "world"

world_map = {}


function world_map:init()

	print("initialising world_map")
	world_map.main_map = world.newWorld("/data/images/background.png", "/data/images/world_spritesheet.png")

end

function world_map:enter(previous)

	print("entering world_map")
	
end

function world_map:draw()

	print("drawing world_map")
	world_map.main_map:draw()
	entities:draw()

end
Since the issue lies in the gamestate variable and its scope, I've read that I should initialise all the global variables in an init.lua file, will this solve this issue?

Re: Some clarification on HUMP.gamestate needed

Posted: Thu Jul 31, 2014 9:52 am
by pielago

Re: Some clarification on HUMP.gamestate needed

Posted: Sat Aug 02, 2014 3:37 pm
by Chroteus
You define gamestate variable after requiring menu, thus it can't see gamestate variable.

The top of your main.lua should look like this:

Code: Select all

gamestate = require "hump.gamestate"

require("entities")
require("menu")
require("world_map")