Sign in to follow this  
Nausea

Game states?

Recommended Posts

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

Share this post


Link to post
Share on other sites
Khatharr    8812
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:[list]
[*]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)
[/list]
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

Share this post


Link to post
Share on other sites
PwFClockWise    512
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 ^^!

Share this post


Link to post
Share on other sites
xinfinite33    204
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

Share this post


Link to post
Share on other sites
Nausea    258
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 :)

Share this post


Link to post
Share on other sites
Serapth    6671
My [url="http://www.gamefromscratch.com/page/Game-from-Scratch-CPP-Edition-Part-2.aspx"]C++ tutorial series[/url] 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.

Share this post


Link to post
Share on other sites
Nausea    258
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

Share this post


Link to post
Share on other sites
Serapth    6671
[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?
[/quote]

That ultimately depends on you.

Share this post


Link to post
Share on other sites
PwFClockWise    512
[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?
[/quote]

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.

Share this post


Link to post
Share on other sites
Nausea    258
[quote name='PwFClockWise' timestamp='1354313395' post='5005862']
[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?
[/quote]

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.

Share this post


Link to post
Share on other sites
L. Spiro    25621
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: [url="http://lspiroengine.com/?p=351"]General Game/Engine Structure[/url]
And how to pass data to them: [url="http://lspiroengine.com/?p=361"]Passing Data Between Game States[/url]


[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?
[/quote]
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

Share this post


Link to post
Share on other sites
Nausea    258
[quote name='L. Spiro' timestamp='1354314654' post='5005870']
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: [url="http://lspiroengine.com/?p=351"]General Game/Engine Structure[/url]
And how to pass data to them: [url="http://lspiroengine.com/?p=361"]Passing Data Between Game States[/url]


[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?
[/quote]
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
[/quote]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this