Some clarification on HUMP.gamestate needed [Solved]

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
fedexist
Prole
Posts: 6
Joined: Mon Jul 28, 2014 7:23 pm

Some clarification on HUMP.gamestate needed [Solved]

Post 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)?
Last edited by fedexist on Sun Aug 03, 2014 3:15 pm, edited 1 time in total.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Some clarification on HUMP.gamestate needed

Post by Kingdaro »

The enter key is actually "return". https://www.love2d.org/wiki/KeyConstant
fedexist
Prole
Posts: 6
Joined: Mon Jul 28, 2014 7:23 pm

Re: Some clarification on HUMP.gamestate needed

Post by fedexist »

Oh. That's it? Well, damn, that was very stupid on my part. Thanks for the reply :)
fedexist
Prole
Posts: 6
Joined: Mon Jul 28, 2014 7:23 pm

Re: Some clarification on HUMP.gamestate needed

Post 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?
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: Some clarification on HUMP.gamestate needed

Post by pielago »

User avatar
Chroteus
Citizen
Posts: 89
Joined: Wed Mar 20, 2013 7:30 pm

Re: Some clarification on HUMP.gamestate needed

Post 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")
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 79 guests