Jump to content

  • Log In with Google      Sign In   
  • Create Account


Game states?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 30 November 2012 - 07:49 AM

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 :)

Sponsor:

#2 Khatharr   Crossbones+   -  Reputation: 2956

Like
2Likes
Like

Posted 30 November 2012 - 08:09 AM

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.

Edited by Khatharr, 30 November 2012 - 08:16 AM.

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.

#3 Celiasson   Members   -  Reputation: 494

Like
1Likes
Like

Posted 30 November 2012 - 08:13 AM

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

#4 xinfinite33   Members   -  Reputation: 203

Like
1Likes
Like

Posted 30 November 2012 - 10:17 AM

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

#5 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 30 November 2012 - 01:31 PM

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 :)

#6 Serapth   Crossbones+   -  Reputation: 5309

Like
0Likes
Like

Posted 30 November 2012 - 02:02 PM

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.

#7 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 30 November 2012 - 03:04 PM

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?

Edited by Nausea, 30 November 2012 - 03:04 PM.


#8 Serapth   Crossbones+   -  Reputation: 5309

Like
0Likes
Like

Posted 30 November 2012 - 03:43 PM

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.

#9 Celiasson   Members   -  Reputation: 494

Like
0Likes
Like

Posted 30 November 2012 - 04:09 PM

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.

#10 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 30 November 2012 - 04:17 PM


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.


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.

#11 L. Spiro   Crossbones+   -  Reputation: 13157

Like
2Likes
Like

Posted 30 November 2012 - 04:30 PM

The answer to your first question is to use one state.

That state can load any level. Just pass a number to that state so that it knows which level to load.
I have outlined how states work here: General Game/Engine Structure
And how to pass data to them: Passing Data Between Game States


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?

Generally yes. If you know you are revisiting the same stage again, that usually only happens in special-case circumstances such as via the “Restart” pause-menu option. These cases can be handled by simply not leaving the current game state etc., so the general policy is to catch the most common cases that cause the same stage to be played again and handle them separately, while otherwise just unloading and loading on any other stage-change.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#12 Nausea   Members   -  Reputation: 257

Like
0Likes
Like

Posted 30 November 2012 - 05:35 PM

The answer to your first question is to use one state.

That state can load any level. Just pass a number to that state so that it knows which level to load.
I have outlined how states work here: General Game/Engine Structure
And how to pass data to them: Passing Data Between Game States



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?

Generally yes. If you know you are revisiting the same stage again, that usually only happens in special-case circumstances such as via the “Restart” pause-menu option. These cases can be handled by simply not leaving the current game state etc., so the general policy is to catch the most common cases that cause the same stage to be played again and handle them separately, while otherwise just unloading and loading on any other stage-change.


L. Spiro


Thank you for the advice, been reading some of those blog posts. :)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS