Untitled Sheep Game Update : Implementing a saving system from scratch

3 minute read

I devoted the time since the last update on implementing a saving system into the game.

Actually the implementing part was turned into learning about the act of Serialization which was made into “learning” from a random github repo and when I say “learning” I mean blatantly copying cause I didn’t have a clue what I was supposed to do so I needed something to start, and actually end, somewhere.

So what I found happens to be one of the many ways you can implement a save system and it goes like this :

At a fundamental level there is a base SaveData class that acts as the storage of the values you want to keep between sessions. The SaveGameManager provides the actual implementation of Save and Load methods that will be used to perform acts of…saving and loading. The Save method opens a file stream, because we need an actual file in the disk to store our desired values, and via a binary formatter turns our Save Data class into a binary that is saved via the stream we opened into a file in the disk.

On the other hand the Load method deserializes the file, meaning that we go the opposite direction than before, going from a binary to an actual Save Data class.

Pretty cool way to track progress if you ask me !

That is where the part of our fellow programmer that was very kind and awesome to share his save manager script. For the purposes of my game I had to add some extra functionality in the form of saving positions of the poop prefabs in the scene that will carry over, if left uncleaned, between different game sessions. My trail of thought was that maybe the player won’t have the time to complete all the acts of feeding, petting AND cleaning so some poop may be left there.

At first I thought that a Vector3 List would be perfect for storing the positions of the prefabs but upong trying to do so, I encountered the problem of Vector 3 not being a Serializable type in Unity. Really strange so I have to investigate further in the future, but the first thought was to create a custom type-class that will store this values. The solution came in the name of the PoopPosition class that stores 3 floats(x, y and z) that correspond to the positions of the poop prefabs found in scene.The later is accomplished via a simple scan in scene which locates the gameobjects with the tag “poop” in it and storing them in the List<PoopPosition> poopPositions of our SaveData class, each time appending the new positions found. But wait there is more !

Appending the positions could lead to creating duplicates of uncleaned prefabs between sessions. For example if there are 2 poop prefabs in our scene and we close the game and reload it, at first we will have 2 again. Correct ! Now again, don’t clean anything and just save(quit) and relaunch. Now the prefabs are 4! That happens because we append the same prefab positions in our unflushed List<PoopPosition> poopPositions.

The solution to this was simple and done via poopPositions.Clear() after the instantiation phase and the loading part in our Start() function. That way after each load of the save file we have a squeeky clean list to populate again!

I can say that the whole thing was a rollercoster of emotions and debugging since everytime I made something work, there were two more minor things that I need to do so the save system works perfectly all the time and for different scenarios, and from this I gained so much new knowledge!

That’s it for now and I will see you in the next update!

P.S. I really need to get deeper into serialization and how the whole serializable attribute works and it’s in my plans now that I got a first taste!

teaser

Updated: