CANNOT GO TO STATES THAT IS ALREADY LOADED

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
User avatar
JME
Prole
Posts: 7
Joined: Mon Mar 04, 2013 7:49 pm
Location: Gothenburg, Sweden

CANNOT GO TO STATES THAT IS ALREADY LOADED

Post 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.
If you understand me or not, that is not my problem.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: CANNOT GO TO STATES THAT IS ALREADY LOADED

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

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 9 guests