Game states?

Started by
10 comments, last by Nausea 11 years, 4 months ago
So this is a pretty simple question.

Since I would use game states for Intro, Menu, In-game etc.
Would I have a separate game state for every level in the game or just a "in-game" state which loads every level?

And if it´s just one game state, how would this work when you only want to load a level once it´s going to be used.
So you don´t load ten levels at the same time just to play the first level.

Thanks :)
Advertisement
Typically an individual game state is one of several available methods for creating an interface between the user and the game's "hidden" persistent state.

For instance, for an RPG you're walking around on the world map and you hit the button to being up the menu. You change game states to do this. Once you're done checking your items you close the menu. Now if you were storing your player's position and etc in the map state then you're going to find a problem here since when closing the map state you lost all that information. (Unless you're using a stack-style state manager, which is something that was mentioned to me recently and sounds like it would be fun to try out.)

If you have a map class that encapsulates that information then you can use your map state to represent and interact with that information. Then when you close the menu you just switch back to the map state and it can pick up right where it left off because the map class object is part of the persistent state rather than the gamestate. Likewise, if you move to a new map your map state just fades the screen, replaces the current map class object, then fades the screen back in.

Having your game states clearly separated from the game's persistent state (this is why I call them 'scenes' instead of 'game states', lol) also makes it a lot easier to do things like saving and loading the game. All of the data that you would want to save or load will belong to the persistent state rather than the scene/gamestate.

Typically I assign the following responsibilities to a scene/gamestate:

  • load or acquire the graphics, audio and other resources required to represent the state
  • react to user input
  • provide the user with a means of affecting the persistent state (the menu scene gives an interface for changing the inventory, the map scene changes your location, etc)
  • represent the persistent state in a meaningful way to the player using the audio/video/etc resources that were loaded
  • perform logic, including trigger AI updates or trigger the update of any continual process in the game world, such as physics, etc (depending on the game type these things may be triggered outside for an un-pausable game)

That's the general idea as I see it, anyway. This is a subject that I'm presently reviewing, however.

Hope that helps.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Several game states:

Well, a game state should hold all the game logic. The way you COULD do this is to have game state with sub states that are called like level1State, level2State etc. The problem here is that when doing level transitions I'm guessing you'd like something like "Now entering level 2" or something, and that is more like a menu state and when entering that state you will remove the game state which could cause problems. There are probably several ways to get around this as well, but I can't really think of one on the top of my head^^

One game state:

I guess you need something that indicates that you would load the next level. As you said, you don't want to load all levels at once. I can't really see what the problem here is since you just need to divide the logic on what to load and what to not load depending on what level the player is at. I'm just brain storming here, but you could have a class for each level and just create instances for each level when needed, I guess ^^

I'm not that good at programming and haven't used states yet, even if I know the basics concerning it.

Hope I was of any help ^^!
Hi there! I'm also working on a state based engine and the way I'm doing it is that when I call my engine's ChangeState() function it accepts a gamestate object and an integer value as an argument ex: Function ChangeState( CgameState * state, Int i). each state can use this value how it wants. for example if I pass in the "in-game" state, I use the integer value to specify which level I want to load.The levels themselves are stored seperately as "level" objects, not as states. With this method, Loading/Switching to level 3 is as simple as calling Changestate(inGame,3). If you want to keep data that is constant between states, I would suggest creating a class that holds the game data. Whenever you load a new level you just read the data from the GameData class. I hope This helps :D
Ok thank you all for your thoughts/tips. I will have to think about this some more before I go ahead and implement something. Thank you :)
My C++ tutorial series demonstrates exactly what you are describing. It uses game states to control transitions between Main Menu, Playing, etc.. It might be worth checking. The basics are shown in part two ( what I linked ), but involved upon as the tutorial progresses.
I got another question now.
When I leave a state, for example a stage in the game, should I release the assets for the stage from my asset manager even if I would enter the stage again later?

I got another question now.
When I leave a state, for example a stage in the game, should I release the assets for the stage from my asset manager even if I would enter the stage again later?


That ultimately depends on you.

I got another question now.
When I leave a state, for example a stage in the game, should I release the assets for the stage from my asset manager even if I would enter the stage again later?


It depends. I believe you would want to release the state you're exiting, since all you want your state machine to do is create, enter, exit and release states. There is no meaning in keeping track of a state you're not currently in, in my opinion. However, you could also use back compatibility in several different situations. If you are in the game state and then presses the key that enters the menu state, of course you would like to be able to resume playing without having to restart the game.

[quote name='Nausea' timestamp='1354309463' post='5005843']
I got another question now.
When I leave a state, for example a stage in the game, should I release the assets for the stage from my asset manager even if I would enter the stage again later?


It depends. I believe you would want to release the state you're exiting, since all you want your state machine to do is create, enter, exit and release states. There is no meaning in keeping track of a state you're not currently in, in my opinion. However, you could also use back compatibility in several different situations. If you are in the game state and then presses the key that enters the menu state, of course you would like to be able to resume playing without having to restart the game.
[/quote]

I guess I could do it for when a stage reaches "completed", when the game wont return to that stage at least at this time?
And for in-game menu etc I keep the other states assets.

This topic is closed to new replies.

Advertisement