How do you code 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.
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

How do you code game states?

Post by bgordebak »

I'm making game states using booleans. I don't think what I do is the best way though. Here's generally how I do it:

Code: Select all

function love.update()
	if love.keyboard.isDown("space") and issplash == true then
		issplash = false
	end
end

function love.draw()
	if issplash == true then
		--draw the splash screen
	else
		--draw the level
	end
end
Is there a better practice?

Edit: You see, usually there are a lot of booleans like this in my games. Like isfinished, isdead, islevelup, etc. I'm an old time programmer, but don't know lua enough to code well.

Edit: I think I can make a gamestate variable and check its value, I can't think of anything better. It wouldn't shorten the code for sure, but it would be more readable I guess.
Last edited by bgordebak on Thu Feb 09, 2017 6:58 pm, edited 2 times in total.
User avatar
peterrust
Prole
Posts: 42
Joined: Thu Dec 29, 2016 8:49 pm
Location: Bellingham, WA, USA
Contact:

Re: How do you code game states?

Post by peterrust »

bgordebak,

A lot of people use this gamestate library: https://github.com/vrld/hump/blob/maste ... estate.rst

Even if you don't use the library, you may find the concept useful -- to group together gamestate data that has the same lifetime. In other words, all the data points that relate to the entire game could be in one table, all the ones that relate to a particular level could be in another, etc. That way they can be saved and restored in useful ways.

That said, if you're not saving/restoring then there's not necessarily a need to do anything other than independent booleans. Putting them in a table might help you, personally, for organization purposes, for example if you initialize the table with all the defaults at the beginning of your game:

local state = { isfinished = false, isdead = false, islevelup = false }
if state.isfinished then -- ...

That may help you keep track of what all the different properties are and be a little more organized with them.
User avatar
bgordebak
Party member
Posts: 130
Joined: Thu Jul 10, 2014 2:04 am
Location: Ankara, Turkey

Re: How do you code game states?

Post by bgordebak »

Thanks a lot! I didn't make a game with saving/restoring functions yet, but I might in the future. I'll look into the library.

And using a game state table makes much more sense than my method, and much more "lua-ish". Thanks! I think I will use this approach from now on.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How do you code game states?

Post by Robin »

Also, instead of issplash == true you can just say issplash
Help us help you: attach a .love.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How do you code game states?

Post by Jasoco »

I made my own state machine I call a StateStack. Where you can push states onto it and stack them on top of each other while optionally having previous states pause their update or keep updating while in the background as well as remove states or replace the entire thing completely if needed to.

Then I can have a bunch of states that all do their own thing. One for the title screen, one for the game, maybe I can have a game base state that handles all the game data like game globals and stats while I have separate states for the game map, game battle, shops, etc. Then states for inventory and other stuff, a game over state, pause state that gets pushed over the game, or if I'm feeling like taking a break from my main project and want to fool around with an experiment or code a different smaller project, I can make a new fresh state, set that to be pushed to the stack for a while, and play around in there until I want to go back to the original project without having to create a brand new project just to experiment.
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: How do you code game states?

Post by Ulydev »

Jasoco wrote: Thu Feb 09, 2017 6:54 pm I made my own state machine I call a StateStack
Sounds interesting! Do you happen to have any demo code?
roramigator
Prole
Posts: 2
Joined: Fri Jul 15, 2022 6:59 pm

Re: How do you code game states?

Post by roramigator »

I do it as follows, I'm not sure how efficient is it, but is convenient.

the default game loops are `love.load`, `love.update(dt)`, and `love.draw`; you can replace the value of these functions for your own:

Code: Select all

function love.load()
end
function love.update()
	if love.keyboard.isDown("escape") then
		love.draw = stateTwo
	end	
end
function love.draw()
	love.graphics.print("stateOne")
end

-- custom draw function
function stateTwo()
	love.graphics.print("stateTwo")
end
since function `stateTwo` will be replacing the draw function, you should treat it as if it was just another draw function; in this way you can setup states on different files.

Code: Select all

-- game.lua
game = {}
function game.load()
	love.draw = game.draw
end
function game.draw()
	love.graphics.print("game")
end
return game

Code: Select all

-- menu.lua
require "game"
menu = {}
function menu.load()
	love.update = menu.update
	love.draw = menu.draw
end
function menu.update(dt)
	if love.keboard.isDown("escape") then
		game.load()
	end
end
function menu.draw()
	love.graphics.print('press "escape"')
end
return menu

Code: Select all

-- main.lua
require "menu"
function menu.load()
	menu.load()
end
try it :awesome:
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How do you code game states?

Post by zorg »

roramigator wrote: Fri Jul 15, 2022 7:21 pm ...
I really wonder what is with people answering years old dead threads... are you just bored that you google search something and then write an essay to a random post, or did you lose a dare or what? :huh:
Me and my stuff :3True 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.
User avatar
Gunroar:Cannon()
Party member
Posts: 1142
Joined: Thu Dec 10, 2020 1:57 am

Re: How do you code game states?

Post by Gunroar:Cannon() »

zorg wrote: Sat Jul 16, 2022 12:52 am
I really wonder what is with people answering years old dead threads... are you just bored that you google search something and then write an essay to a random post, or did you lose a dare or what? :huh:
I lost a dare, don't know about roramigator.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
roramigator
Prole
Posts: 2
Joined: Fri Jul 15, 2022 6:59 pm

Re: How do you code game states?

Post by roramigator »

zorg wrote: Sat Jul 16, 2022 12:52 am
roramigator wrote: Fri Jul 15, 2022 7:21 pm ...
I really wonder what is with people answering years old dead threads... are you just bored that you google search something and then write an essay to a random post, or did you lose a dare or what? :huh:
Did I offend you? I just had the same problem and couldn’t find an answer, when I discovered a way I thought to share and maybe someone can benefit from it, is this not allowed?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests