Share your game state management tips!

0 favourites
  • 8 posts
From the Asset Store
State Machine
$10.99 USD
State Machine is a great tool for managing the state of anything in your game.
  • 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?

  • 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.

  • 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!

  • 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.

  • 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

  • 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).

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)