Page 1 of 1
Can someone give me some straight up way to make simple game states?
Posted: Sun Jul 22, 2018 11:32 pm
by ThatChillHomie
I have been looking for ways to make game states in my game since I am new to programming but they all seem so ridiculous. Can one of you guys point me to the best simple way to do it? thanks
Re: Can someone give me some straight up way to make simple game states?
Posted: Mon Jul 23, 2018 2:55 am
by pgimeno
Re: Can someone give me some straight up way to make simple game states?
Posted: Sat Aug 04, 2018 6:53 pm
by Titousensei
Here's a technique I like to use. It's very simple, you just implement each state in its own .lua file as if it were main, and let main.lua dispatche the callbacks to the current state. (This example uses globals, but you can also do "local menu = {} ... return menu" if you prefer.)
main.lua
Code: Select all
require "menu"
local state = nil
function change_state(s)
state = s
state.load()
end
function love.load()
print("Love version:", love._version, love._os)
love.graphics.setBackgroundColor(0,0,0)
-- Do more global inits here
-- Then jump to the menu
change_state(menu)
end
function love.draw()
state.draw()
end
function love.update(dt)
state.update(dt)
end
function love.mousepressed(x,y,b)
if state.mousepressed then
state.mousepressed(x,y,b)
end
end
function love.mousereleased(x,y,b)
if state.mousepressed then
state.mousereleased(x,y,b)
end
end
function love.keypressed(key)
if key == "escape" then
if state==menu then
love.event.quit()
else
change_state(menu)
end
else
state.keypressed(key)
end
end
menu.lua
Code: Select all
-- loading the states I need here
require "game"
require "credits"
menu = {}
local selected = 1
function menu.draw()
-- draw menu
-- highlight selected
end
function menu.update(dt)
end
function menu.keypressed(key)
-- handle menu controls
if key == "return" or key == " " then
if selected == 1 then
change_state(game)
elseif selected == 2 then
change_state(credits)
end
elseif key == "up" then
selected = 1
elseif key == "down" then
selected = 2
end
end
credits.lua
Code: Select all
credits = {}
function credits.draw()
-- draw menu
end
function credits.update(dt)
end
function credits.keypressed(key)
-- any key goes back to menu
change_state(menu)
end
Re: Can someone give me some straight up way to make simple game states?
Posted: Sun Aug 05, 2018 4:46 am
by zorg
Titousensei wrote: ↑Sat Aug 04, 2018 6:53 pm
Here's a technique I like to use. It's very simple, you just implement each state in its own .lua file as if it were main, and let main.lua dispatche the callbacks to the current state. (This example uses globals, but you can also do "local menu = {} ... return menu" if you prefer.)
(...)
That's cool and simple enough and all, but i think your solution that you've shown has at least one issue, at a glance:
- You're not guarding against the state functions not being defined; now you might say that it should be self-evident that update and draw
must be, but you yourself call state.load in your change_state function, yet none of the states have a load function defined, which will error since you expect that to exist.
Re: Can someone give me some straight up way to make simple game states?
Posted: Sun Aug 05, 2018 4:59 am
by Titousensei
zorg wrote: ↑Sun Aug 05, 2018 4:46 am
That's cool and simple enough and all, but i think your solution that you've shown has at least one issue, at a glance:
- You're not guarding against the state functions not being defined; now you might say that it should be self-evident that update and draw
must be, but you yourself call state.load in your change_state function, yet none of the states have a load function defined, which will error since you expect that to exist.
Ahah! True, this is not a complete solution, just an example of a method. I tried to show different options: mousepressed does guard against undefined, whereas draw and update do not. Some callbacks will be defined, some will not, depending on your project. It's simple enough to add the ifs when necessary.