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.
A Problem loading gamestates
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
A Problem loading gamestates
- Attachments
-
- tictactoe.love
- (1.21 KiB) Downloaded 177 times
Re: A Problem loading gamestates
This first:
main.lua :
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:
game.lua:
You could do the same thing you do with the load and draw functionsAnother 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?
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
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
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
I'm sorry about my poor wording! I'll give an example.
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.
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
of course, because lets say your beginning state is "start". In the love.load function you do this:
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.
Hope it isn't that hard to understand, sorry for the bad explanation.
Code: Select all
if state == "start" then
start.load()
end
if state == "menu" then
menu.load()
end
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
Last edited by MadByte on Mon Jun 24, 2013 11:06 pm, edited 2 times in total.
Re: A Problem loading gamestates
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.
I greatly appreciate the help, and I'm very sorry that I'm having trouble understanding it.
Re: A Problem loading gamestates
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
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
Who is online
Users browsing this forum: dusoft, Google [Bot] and 4 guests