Skip to main content
GameDev.net gamedev.net
🔒 Locked

Passing data between game states

Started by Staggy11 May 4, 2009 at 5:25 AM 1 replies 1k views
Original Post
Staggy11
Staggy11
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.
Zipster
Zipster
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.
Staggy11
Staggy11
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.