Page 1 of 1

A Problem loading gamestates

Posted: Mon Jun 24, 2013 10:01 pm
by Bransonn
The name in the topic seems like something I should have just searched the forums for. I did, and haven't found any information on my exact problem.

I'm making a Tic-Tac-Toe clone. I started to work on a menu system, and it was all working fine and dandy for awhile. But now, for some odd reason, the third menu will not recognize any variables in my menu.load(). It will recognize variables in my menu.draw(), but not load. Wording this is a bit tricky. Attached is my .love file so you can see what I mean.

Another question I have is with the love.mousepressed function. It seems that if I use it in one state, it carries over to the next. An example would be with my current .love file. Say I make menu have it's own love.mousepressed function. When I do that, the love.mousepressed function in my start gamestate won't work anymore. I changed it the other night to "start.mousepressed" and it worked for awhile. Then it just STOPPED working. My question is, how can I make this function work in multiple states?

Thanks in advance.

Re: A Problem loading gamestates

Posted: Mon Jun 24, 2013 10:16 pm
by MadByte
This first:
Another question I have is with the love.mousepressed function. It seems that if I use it in one state, it carries over to the next. An example would be with my current .love file. Say I make menu have it's own love.mousepressed function. When I do that, the love.mousepressed function in my start gamestate won't work anymore. I changed it the other night to "start.mousepressed" and it worked for awhile. Then it just STOPPED working. My question is, how can I make this function work in multiple states?
You could do the same thing you do with the load and draw functions

main.lua :

Code: Select all


function love.mousepressed( x, y, button )
    if state == "menu" then
        menu.mousepressed( x, y, button )
    end
   -- And for the other states the same
end
I didn't understand your first question i guess. You might forget to reload the menu.load() after you change the state ?

Here is an example on how I manage states, it might help you understand:

main.lua:

Code: Select all


require( "game" )


local State


function setState( state )
	if State then State:leave() end
	State = state
	if not State.loaded then
		State:init()
		State.loaded = true
	end
	State:enter()
end


function love.load()
	setState( game ) -- automatically loads the state and set it
end


function love.update(dt)
	State:update(dt)
end


function love.draw()
	State:draw()
end


function love.keypressed(k)
	State:keypressed(k)
end


function love.mousereleased(x, y, b)
	if State.mousereleased then State:mousereleased(x, y, b) end
end
game.lua:

Code: Select all



game = {}


function game:init()
end


function game:enter()
end


function game:leave()
end


function game:update(dt)
end


function game:draw()
	love.graphics.print( "Hello WOrld!", 10, 10 )
end


function game:keypressed(k)
	if k == "escape" then love.event.quit() end
end

Re: A Problem loading gamestates

Posted: Mon Jun 24, 2013 10:32 pm
by Bransonn
I'm sorry about my poor wording! I'll give an example.

Code: Select all

main.lua

require ('start')
require ('menu')



function love.load()
state = "start"

if state == "start" then
start.load()
end

if state == "menu" then
menu.load()
end

function love.update(dt)

if state == "start" then
start.update(dt)
end

if state == "menu" then
menu.update(dt)
end

function love.draw()

if state == "menu" then
menu.draw()
end

if state == "start" then
start.draw()
end

end

When the state changes from Start to Menu, it stops working. It won't recognize any variables from the menu.load(). I keep getting a nil value.

Re: A Problem loading gamestates

Posted: Mon Jun 24, 2013 10:39 pm
by MadByte
of course, because lets say your beginning state is "start". In the love.load function you do this:

Code: Select all

if state == "start" then
start.load()
end

if state == "menu" then
menu.load()
end
You may forget that the love.load function is only called once un start up, that means if you set the gamestate to "menu" after starting the program the if state == "menu" then loop does not get called again.

You have to load the load function of a state along with your state change or you have to preload all load functions.
If you look at my example you will see a way to solve this problem by using a function to set your state which load the init function after switching to the state.

Code: Select all

function setState( state )
   if State then State:leave() end
   State = state
   if not State.loaded then
      State:init()
      State.loaded = true
   end
   State:enter()
end
Hope it isn't that hard to understand, sorry for the bad explanation.

Re: A Problem loading gamestates

Posted: Mon Jun 24, 2013 10:56 pm
by Bransonn
I'm really sorry.. but I don't understand " states.init function". What is an .init function? Could you possibly link me something so I can better understand this?

I greatly appreciate the help, and I'm very sorry that I'm having trouble understanding it.

Re: A Problem loading gamestates

Posted: Mon Jun 24, 2013 11:00 pm
by MadByte
sorry,

init is just another name for load(stands for initialize)... many programmers using this name, instead of i.g "menu.load() it would be "menu.init()"
sorry for confusing you ;)