Page 1 of 1

Switching gamestates

Posted: Tue Aug 23, 2011 8:49 am
by Ezzam
This is my first post here, so hello everyone. I've got a problem with gamestates. Here's the main.lua:

Code: Select all

require 'game'
require 'menu'

game.load()

function love.load()

end

function love.update(dt)
currstate.update(dt)
end

function love.draw()
currstate.draw()
end

currstate = menu
First, i load game.lua so there won't be any errors. My game is based on love.physics. Game.lua is the level of the game, and i want to have menu done exactly the same way as level, but when i load menu gamestate, objects defined in game.lua are still on their places. Is there any way to unload a state, then load a new one?

Re: Switching gamestates

Posted: Tue Aug 23, 2011 8:54 am
by Robin
You write a function to unload a state and call it?

Re: Switching gamestates

Posted: Tue Aug 23, 2011 9:02 am
by T-Bone
It kind of depends on your game. The way I do it in my own is that every state has a :start()-method, which resets the state and starts it up. There isn't really a need to unload an old state unless it takes up large amounts of memory, just reset the state when you switch to it.

Re: Switching gamestates

Posted: Tue Aug 23, 2011 9:13 am
by vrld
With hump.gamestate there is a leave function that will be called when you switch the gamestate. This can be used to clean up the physics world or remove stuff from the scene manager (if you have one).