Page 1 of 2

How do you code game states?

Posted: Thu Feb 09, 2017 12:41 am
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.

Re: How do you code game states?

Posted: Thu Feb 09, 2017 1:39 am
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.

Re: How do you code game states?

Posted: Thu Feb 09, 2017 1:43 am
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.

Re: How do you code game states?

Posted: Thu Feb 09, 2017 10:15 am
by Robin
Also, instead of issplash == true you can just say issplash

Re: How do you code game states?

Posted: Thu Feb 09, 2017 6:54 pm
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.

Re: How do you code game states?

Posted: Sat Feb 11, 2017 11:14 am
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?

Re: How do you code game states?

Posted: Fri Jul 15, 2022 7:21 pm
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:

Re: How do you code game states?

Posted: Sat Jul 16, 2022 12:52 am
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:

Re: How do you code game states?

Posted: Sat Jul 16, 2022 11:11 pm
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.

Re: How do you code game states?

Posted: Mon Jul 18, 2022 3:01 am
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?