Share your game state management tips!

Discussion and feedback on Construct 2

Post » Thu Apr 06, 2017 11:25 pm

Hey, so I've been spending time trying to figure out what the best way to manage game states are as my projects become larger.
So why not put the question out to the community and see what everyone's opinions are on how to manage your game states and keep your projects clear and concise.

For example, do you use groups to separate game states? if so, how do you manage them? Do you have any organizational patterns for your groups/events, etc?
B
41
S
19
G
65
Posts: 1,085
Reputation: 37,842

Post » Thu Apr 06, 2017 11:34 pm

Use a dictionary for variables, and store the dictionary into local storage as json.
This will save you a lot of angst if you plan on having game saves.
Image ImageImage
B
168
S
50
G
163
Posts: 8,221
Reputation: 105,061

Post » Fri Apr 07, 2017 2:09 am

I use groups and functions extensively. And I like my code to condense down to nothing. Any code I am not actively working on gets minimized. And if I am not using any of the functions in a group, the whole group gets minimized. The current project I am working on has 1374 events on the main eventsheet (and growing), but when I load the project, it fits on one screen. I arrange the functions in a group alphabetically, and I always start code for functions from a blank sub-event (so that when the function is minimized all you see is the function name. The first line in the function is a comment that lists the parameters to pass in. With a couple clicks I can expand exactly what I need to see - its very quick and easy to find things, and everything else is hidden. Groups are divided up by what they do: Touch, Mouse, Keyboard, Player, Enemy, etc... but they are mostly general purpose routines, rather than for specific states. I will normally have a global variable that determines the state and that controls what code runs.

I hate having unnecessary code running. If nothing is happening on screen, then cpu utilization better be pretty close to zero. Only do what's needed - when it is needed!
B
94
S
33
G
20
Posts: 438
Reputation: 17,615

Post » Fri Apr 07, 2017 5:38 am

Thanks for the suggestions!
I like the idea of organizing the functions alphabetically.
I've been minimizing everything recently too. It definitely helps traversing through a project.
A comment for function parameters is a good idea- I'll have to remember to do that.
I too am trying to find ways to prevent code from constantly running. At the moment, I'm creating a blank project just to see if I can come up with some generic system that will encourage that. At the moment I'm thinking I'll have a set of groups that run continuously, and then a set of groups that can only have one group from the set active at a time. Then I'm going to try to put stuff that needs precedence(they should be the only thing happening) into that group set. Still experimenting with it, but I hope that it will prevent things from becoming entangled- so that I can keep adding more without concern with breaking something.
One thing I've used more are local variables in groups, and setting them static so that they don't reset their values. I use to have the habit of adding variables to objects, but now I try to limit those whenever I can.
B
41
S
19
G
65
Posts: 1,085
Reputation: 37,842

Post » Fri Apr 07, 2017 5:05 pm

Groups are an AWESOME compartmentalization tool. Another one I'm fond of is using just an absurd amount of event sheets for further de-tangling. All my UI stuff is in my UI evSheet. The one downside is I keep forgetting to include the new event sheets in to the gameloop's main event sheet, so I'll spend twenty minutes coding something, try it out, and have it not work, then spend twenty minutes going through my own code trying to figure out what I messed up, until someone watching my stream goes "Hey Brendan, remember when you told us to remind you to check if you included the event sheet? Well, did you remember to include the event sheet?"

I then bang my head on my desk, fix the problem, and get back to coding. xD
B
18
S
3
G
3
Posts: 82
Reputation: 2,543

Post » Sun Apr 09, 2017 3:00 am

Yeah, if I use a lot of eventsheets I forget to include them sometimes. The thing I dislike about event sheets is the limited amount of tabs that can be shown in c2- so it makes it difficult to flip through them if you have too many open. But they are definitely useful.
I'm also learning that it is good to use separate arrays for specific things instead of mixing different things in one big array. By having smaller arrays that are designed for one use, you can also name it more appropriately, and it is less confusing to retrieve data from them, and also store data (you don't have to keep track of which index stores what, etc).
B
41
S
19
G
65
Posts: 1,085
Reputation: 37,842

Post » Sun Apr 09, 2017 2:32 pm

My larger projects generally have a global invisible sprite with dosens of booleans and variables determining a variety of games states; where the booleans are checked whether or not pieces of code should run and the variables can contain json strings from arrays or smaller variables such as lvl, health bullets etc.

Saving the global invisible sprite to local storage asjson gives a great save method.

I use groups mostly to structure my event sheets and have events sheets for specific parts of the game such as controls or FX etc
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,562

Post » Tue Apr 11, 2017 3:37 am

@lennaert , I was using a general global sprite with variables for a while, but for my latest project I'm using a "master" sprite for each part of the game; dialogMaster, menuMaster, etc.. I put them in a family and give the family a state behavior. I'm using those to control which parts run. It seems to be working well.
I'm finding that it becomes easier to expand things when I separate stuff and not try to cram everything into one object or family. If I try to cram stuff together, that just encourages more of that and it becomes a mess.
But since I use more objects now, I also make sure to use folders to organize them all.
B
41
S
19
G
65
Posts: 1,085
Reputation: 37,842


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 5 guests