Page 1 of 1

CANNOT GO TO STATES THAT IS ALREADY LOADED

Posted: Mon Mar 04, 2013 9:25 pm
by JME
I have a problem with my game:

When I run the game, I can only visit a state once.
For example, when I start the game, I am led to the state "meny" (the main menu). From that state I can simply go to another state called "LevelSelect" (a level selecting menu). from that state I have buttons linked to all other states in the game (including the meny state), but I cannot go to the meny state because i already have loaded it. If I try to do that, the LevelSelect state will reload instead. It do not matter which state I am on or which state i try to load.

I have a main main.lua that I load the states from. It looks a bit like this:

Code: Select all

function loadState(name)
	state = {}
	local path = "states/" .. name
	require(path .. "/main")
	load()
end

function load()
end

function love.load()
	loadState("meny")
end
I would be thankful if someone helped me.

Re: CANNOT GO TO STATES THAT IS ALREADY LOADED

Posted: Tue Mar 05, 2013 2:06 am
by Inny
require only loads a file once, so the load function is changing for each state. You should probably define your game lua files like so:

Code: Select all

-- meny.lua (not sure if mispelled)
local meny = {}

function meny.load()
  --
end

return meny
Then, pass around meny directly, rather than via a string name, like so:

Code: Select all

-- main.lua
meny = require('meny')

function loadState(s)
  state = s
  state.load()
end

loadState(meny)