Building a save system for level objects in Unity Pt. 2 - Usable Objects

posted in Christin Kalina
Published February 15, 2019
Advertisement
Building a save system for level objects in Unity Pt. 2 - Usable Objects
 
This builds on https://www.gamedev.net/blogs/entry/2266638-building-a-save-system-for-level-objects-in-unity-pt1-checkpoints/ and I would highly recommend to read through part 1 before continuing with this second part. (You still will understand what is going on, I hope, but I am skipping the underlying save and loading logic here because I went into these details in part 1 already.)
 
As the player moves along in a game and interacts with different objects in the world, we want to reflect his actions in the game by changing the state of objects permanently. For example, an item that was picked up should not appear again.
 
 
Objectives:
  • Save and restore states of objects in the scene according to the progress of the player.
  • These objects are not interconnected.
  • Just remember that this object was used
 
Used programming concepts:
 
Link to first part:
 
 
Unique IDs
1. Finding a persistent unique ID for GameObjects
 
This is very similar to the checkpoint  but instead of using enums to identify the saved state, this approach works with a unique ID per GameObject. So that you do not need to create hundreds of enum entries just because you want your game to remember that token ABC was picked up. GameObjects do not come with a unique ID out of the box. There is Object.GetInstanceID() https://docs.unity3d.com/ScriptReference/Object.GetInstanceID.html but this is exactly only that - an instance id. This ID gets regenerated every time a unity scene is loaded. As we seek for consistent behavior, this will not do.
 
 
2. UUID
 
So, after researching some time in the internet on how to give GameObjects a unique ID, I found Bunny83's UUID: https://github.com/Bunny83/UUID/.
It is a component that will create and serialize a System.Guid for a GameObject - Perfect! 
The way it creates and remembers the Guid across scenes is an interesting process, definitely worth debugging through it to see how it works.
 
 
IPersistentObject
 
Great, now we have a unique ID that will not change when we load scenes. Next question is how to distribute this on the GameObjects that we want to remember. One way would be to just attach it. 
But is there an automatic way? Yes, there definitely is! 
We could work with [RequireComponent] but this only works on objects that have not been added to the scene yet. 
 
Let's build our own system for that then. The method I went for uses an Interface and the OnWillSafeAssets method in Unity. The later is called when a scene is saved. I basically search for all the objects in the scene that implement this interface and add the UUID component if it does not exist yet.
In my case all objects that are interacted with came with a component that handles the logic of interacting with the object like adding stuff to the inventory or some currency counter. I extended this component with implementing my persistence interface. So that I can "mark" all types of components with the IPersistentObject interface that need a unique ID for saving.
 
PersistableObject
 
Now, similar to the checkpoint system, I created a component that handles the saving of the object. It also comes with a unity event and will register itself in a controller that has access to a save object that will be serialized to JSON. Instead of saving the enum though, this will save the UUID of the object. 
The OnWillSafeAssets method not only attaches the UUID but also the PeristableObject component. So that this does not need to be done manually either. 
The saving is then hooked up into the logic when the object is picked up, so that it will not appear again after being picked up once. 
(Have a look at the first part of this series for how approach the saving and loading. )
 
 
Great success!
 
Now with both the enum based and UUID based saving of objects in the scene we have great tools that let the player feel that he is impacting the world with his/her actions.
In the next part I will introduce you to how to integrate this into Unity's editor system for easy debug.

 

2 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement
Advertisement