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

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

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

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 » Thu Feb 09, 2017 10:15 am

Also, instead of issplash == true you can just say issplash
Help us help you: attach a .love.

User avatar
Jasoco
Inner party member
Posts: 3650
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How do you code game states?

Post by Jasoco » Thu Feb 09, 2017 6:54 pm

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: 440
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: How do you code game states?

Post by Ulydev » Sat Feb 11, 2017 11:14 am

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?

Post Reply

Who is online

Users browsing this forum: No registered users and 97 guests