Preserving Scene State
Why Do Items Re-Appear When I Move Between Scenes?
• Each scene we have created has a number of gameobjects that get instantiated every time the scene is loaded.
• These gameobjects include the ‘Items’ that we created for the scene.
• When a scene is first loaded this is fine – all the Items get instantiated.
• Then our player walks around the scene ... collects items and adds them to his inventory ... moves to a different
scene and does the same ... moves back to the first scene and ... all the items he collected before have
reappeared!
• This isn’t an error. Every time a scene loads it instantiates all the gameobjects in the scene that were specified
at design time. By default the scene knows nothing about the items that your player character has collected.
What we need is a way of keeping track of the ‘scene state’ i.e. in this case what items
the player has collected in the scene, so when the player re-visits the scene only the items
that should be there are displayed
Preserving Scene State
We Need A Way To Maintain The ‘State’ Of Scenes
Implement a ‘SaveLoadManager’
SceneControllerManager
BeforeSceneUnloadFadeOut SaveLoadManager
Store Scene Data
BeforeSceneUnload
Restore Scene Data
AfterSceneLoad
AfterSceneLoadFadeIn
Preserving Scene State
We'll Create A Save & Load Manager
SaveLoadManager
Gameobjects That Need To Save Scene State Will
Register Themselves With The SaveLoadManager
Store Scene Data
SceneItemsManager
ISaveable
ObjectList
ISaveable Interface Restore Scene Data
ISaveableRegister
ISaveableDeregister
ISaveableStoreScene
ISaveableRestoreScene
They Will Implement The ISaveable Interface To
Define The Methods They Need To Implement
For Scene Saving (this will be extended in future
lectures to add ISaveableSave and ISaveableLoad
for game saving to file)
Preserving Scene & Game State
Gameobjects That Need To Save Extend In Future Lectures – Scene and Game Saving
Scene & Game State Will Implement
The ISaveable Interface
SceneItemsManager SaveLoadManager
Player Store Scene Data
NPC Restore Scene Data
ISaveable
ObjectList
InventoryManager Save Data To File
TimeManager Load Data From File
GridPropertiesManager
Preserving Scene State
High Level Save System
SceneControllerManager
SaveLoadManager
BeforeSceneUnloadFadeOut
Store Scene Data
BeforeSceneUnload Restore Scene Data
Game UI
Save Data To File Save Game
AfterSceneLoad
Load Data From File Load Game
AfterSceneLoadFadeIn
Preserving Scene State
Using SceneItemsManager To Store Scene Item Data
Future Save Game
Functionality
SceneItemsManager
GameSave
Dictionary
GenerateGUID string ISaveableUniqueID <string,
string GUID GameObjectSave GameObjectSave GameObjectSave>
Globally Unique Identifier
GameObjectSave
Dictionary<string, SceneSave> sceneData
Scene Name
SceneSave
Dictionary<string , List<SceneItem> listSceneItemDictionary
Identifier Name For List
SceneItem
int itemCode
Vector3Serializable Vector3Serializable position
float x, y, z string itemName