## Can someone give me some straight up way to make simple game states?

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.
ThatChillHomie
Prole
Posts: 5
Joined: Sun Jun 10, 2018 1:52 am

### Can someone give me some straight up way to make simple game states?

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
pgimeno
Party member
Posts: 2611
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Can someone give me some straight up way to make simple game states?

Titousensei
Prole
Posts: 13
Joined: Mon Aug 20, 2012 11:35 pm

### Re: Can someone give me some straight up way to make simple game states?

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

zorg
Party member
Posts: 3090
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

### Re: Can someone give me some straight up way to make simple game states?

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.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Titousensei
Prole
Posts: 13
Joined: Mon Aug 20, 2012 11:35 pm

### Re: Can someone give me some straight up way to make simple game states?

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.

### Who is online

Users browsing this forum: No registered users and 16 guests