Sign in to follow this  
Staggy11

Passing data between game states

Recommended Posts

Staggy11    122
I'm using the State pattern in my game, and I've got it working at a basic level. However, I have a bit of a problem. My first few States are okay - Title, Options Menu, etc. However I have just started to add in the actual gameplay states - currently 'World Map' and 'Inventory'. My question is - how should I go about sharing information between the two. For example, how would I share the relevant information such that each character is shown with the correct equipment on the world map, and then that equipment is listed in the Inventory. I don't want to have to go back and add in passing pointers for this at the BaseState level - there's no need for such things in the Title Screen, for example. Obviously all the information would have to be stored at the highest 'gameplay' state, and then distributed down. But how do I go about this? I hope someone can help, as I've canned countless mini projects at this point, either because i cannot see a solution or because any solution makes the code so convoluted as to be impossible to expand.

Share this post


Link to post
Share on other sites
Zipster    2359
I think the issue might be that you're using States for things that should really just be GUI (which in this context is considered a mechanism for manipulating state). For instance, if all your inventory does is manipulate the player's equipment, then it should just be a GUI that can pop up during the Game state. When you're at the main menu, you can't show an inventory screen because there is no player state to modify. However, you may be able to open up a video options GUI at the main menu and during the game, since there are always video settings like resolution and full-screen that exist at the global level above all your other states (or you can think of it as an all-encompassing "Application" state, with all other states being children).

For most games there are really only a handful of states, and they tend to exist at a very high level (main menu / no game, single-player game, multiplayer-game, etc.). If you find yourself adding a lot of states and then having problems with data sharing, you should look for places where states can be merged and how you could accomplish what you were trying to do with states using other mechanisms.

Share this post


Link to post
Share on other sites
Staggy11    122
I think I see; so, if it were a story States would be Chapters, whereas I'm using them for paragraphs.

Thanks for the reply. I'll see what I can do - it certainly sounds easier now.

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