What are the possible ways of sharing different objects between game states?
Lets say a game has a couple of states (a menu state and a game state for example), at first the engine calls the menu state's load function which loads menu textures, music et cetera.
After returning from the load function the engine enters a loop which calls update and render over and over. At this point the menu state makes a TCP connection to a IP (joining a lobby for a 1v1?) which simply creates a object of a connection class and then signals the engine to switch to the game state, the menu state then unloads all the stuff it loaded up.
Now the engine calls game state's load function, the game state loads models, textures et cetera and then enters the update-render loop.
The question now is, how will the game state know about the connection class object that the menu state created? How do the states share objects between themselves? And not only connections, different object types too, even templates maybe, imagine a template class called container which can hold anything such as textures, models, integers, strings, settings under the form of strings that are shared between the menu state and game state such as what game save the player chose when in menu state, or the difficulty...
There are two questions that look like this one but neither of them answered my question:
Answer
You're probably overthinking it. The only things owned by the game state should be things specific to that game state alone. Everything else should exist outside the game state and persists throughout. Usually, whatever owns the game state should also own those items, but it could be any object really. If the game state needs access, pass it in as an argument.
My object hierarchy often looks somewhat like this:
Application
+- Current State
+- World (if it's a gameplay state)
+- Characters
+- Objects
+- Application Settings
+- Graphics/GUI
+- Audio
+- Networking
+- Resource Managers (unless they're part of specific subsystems)
How does the Current State know about the Audio? I pass in a reference, either when I create the state or when I update the state. Same for everything else.
No comments:
Post a Comment