- Home /
Save Data Strategies
I've seen several threads about saving/loading game data, but I'm still feeling like I'm missing something. I know how to save information to files, but I'm a stuck on the general approach to use.
We're making a large game and I expect we'll need several scenes. Let's say one per dungeon level. What approach should I take so players can go up and down the levels and things stay the way they left them? If I killed a goblin, I want the goblin laying in that position when they come back. If a chest was broken into, I want it to still be broken and open.
I understand how to save data to a file, but there are some odd issues:
GameObject IDs change when a scene is loaded. So I can't just save a transform and associate it with an ID.
What about objects that have bones or sub-objects. How should I save the goblin so his rag doll bones are laying in the exact same position?
Objects may now exist when they didn't in the original scene. Maybe there's an arrow I shot and left on the ground...
Objects in the original scene may be totally gone.
How about objects that are unique, but in all the scenes? Assume there is a specific bad guy who is following you around. You may have hurt him in scene #1 and I want him to still be hurt in scene #2 (if he was).
Item 1 Seems like the major issue to me. Since there's no reliable and consistent GUID, I can't reliably store the information in a file or database and map it back to the GameObject in the scene.
Any advice on creating and saving the progress of a larger game more complicated game would be appreciated.
Answer by christoph_r · Jun 06, 2014 at 12:27 AM
Have you watched and understood this video?
GameObjects are serializable, see link text.
If you have a prefab for a dead/unfunctional goblin it's as easy as storing the rotation and position of each limb. If you're not already storing that dead goblin as a serialized GameObject, that is.
See above, store the rotation and position as well as the type of GameObject (how you do that is up to you and your needs, you can make a list of Prefabs and store the index, you could serialise the actual GameObject etc. etc.).
That one is a bit more tricky, but you should be able to reference objets via their transforms and destroy them when you load the level, based on your save-data.
Have you looked at DontDestroyOnLoad?
Anyway, persistent data can be a pretty tricky and daunting subject which might require some careful planning ahead. It's generally a good idea to break it down into smaller parts to get familiar with the concept. You could think of very low level information (say an int and/or a string) you want to store per level, implement that and then move on. Another good place to start or something to try after that would be keeping your "hunter" enemy persistent with DontDestroyOnLoad, as saving to files and using DontDestroyOnLoad are more powerful when they complement each other.
Thanks Chris,
I did watch the video and I get how to save each piece individually. I guess I was looking for how massive amounts of data gets stored and accessed.
In engines I've created myself, I'd have data structures (similar to database tables) that would hold all the information. A "game object" would have a reliable ID that can be used in the tables to hold hit-points, damage, etc. Then loading or saving scenes just looks into the data structures using that ID.
This also allowed me to do global changes in one spot. Think database query to update all swords at once.
In Unity, everything is stored on the object itself and that object's ID isn't reliable. Since it's not reliable, I can't tie data structures back to a specific object.
1: The doc says it can be serialized, but I get this error: UnityEngine.GameObject is not marked as Serializable.
GameObject lCube = GameObject.Find("Cube");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/save.dat");
bf.Serialize(file, lCube);
file.Close();
In some posts, people are saying to serialize each component individually. In that case, I still need to tie the data back to as specific GameObject.
3: I suppose this is easy enough using reflection.
5: I have, but I keep thinking more in terms of database tables...maybe that's my problem. If I knew the boss's unique ID is 123, I'd look it up in the table and see what his hit points should be. Without a reliable ID, I can't do that.
So, I think my question is all co$$anonymous$$g down to: How do you associate external data to GameObjects across play sessions when the GameObjects GetInstanceID() isn't consistant?
$$anonymous$$aybe I just need to create a "Unique ID" component for every object I care to persist...
Your answer
Follow this Question
Related Questions
How to save user levels? 0 Answers
How to save my settings 1 Answer