1

I am struggeling to grasp how to handle save/load of every GameObject in a Scene. I currently save data in a Json and it works fine, the part I'm struggeling with is the logic of saving an entire game state.

For example, I'm making a sandbox game where you can shop trees, hoe ground, plant crops, pick herbs, etc, etc. All of these interactable GameObjects are created from prefabs and that works fine. But I need to somehow save every GameObject and its state (i.e is a crop fully grown or not, etc) to a file that I can save/load every time I exit/enter a scene (or the game).

Take this scene I created below, All tiles (except the tall grass) are interactable, i.e I can cut down those trees, pick the flowers, and I planted some crops that have grown:

enter image description here

I cannot wrap my head around how to best save/load all this information. Say I save every GameObject in a file, then would I read that file and Instantiate each object at runtime every time I enter the scene? Is it even possible to save an entire GameObject and just Instantiate it from file? How would I best go about doing this? And this would also mean a scene would load differently depending on if it's the first time I enter it (Everything is in original state) and any time after that it will load from file (without creating a bunch of duplicates).

I use this method by Programmer to save/load data.

Green_qaue
  • 3,561
  • 11
  • 47
  • 89
  • It's not an easy task to save all objects in the scene. There are plugins on the Assets plugin that will help you if you can't do this. I've done this in the pas and it was complicated but the code cannot be shared because it was done for another person. – Programmer Jul 29 '17 at 11:16
  • I will give you an idea that might get you started. 1.You **can't** save a GameObject. You don't have to either. Just get the Gameobjet name then get all the name of the scripts attached to it. Also get the name of the script's namespace. This requires that you loop through them with recursion and these info as string in a class. – Programmer Jul 29 '17 at 11:18
  • @Programmer Thanks for your advice. Can you recommend a certain plugin? – Green_qaue Jul 29 '17 at 11:19
  • 2.The tricky part is loading this back. As long as you have the component name and the namespace I asked you to save as a string in a class, you can see [this](https://stackoverflow.com/a/42357931/3785314) post I made which simplifies everything. Just pass that information to it and the Component will be recreated from the string name. Lot's of work still have to be done but it is complicated. – Programmer Jul 29 '17 at 11:20
  • 1
    There use to be a plugin I recommend to people for this but I forgot its name. I will let you know when I find it. – Programmer Jul 29 '17 at 11:22
  • @Programmer any luck? :) – Green_qaue Jul 30 '17 at 08:53
  • Nope. I still didn't find it. I think you should give this a try yourself and see where exactly you will get stuck. – Programmer Jul 30 '17 at 11:30
  • @Programmer okay, thanks for trying. I will. One thing, why would I need to get the name of all scripts attached to the gameObject? couldnt I just load the correct prefab depending on the gameObject name in the file, and automatically get the correct scripts that way. – Green_qaue Jul 30 '17 at 14:00
  • You can load the correct prefab depending on the gameObject name. Like I said, when I did this I ran into many issues. **1.** What if you destroyed a script attached that is attached to a prefab? When you load it, the destroyed script will be there which is not good. **2.** What if the GameObject is created from script? Not every gameobject is a prefab. If you don't care about this then go for it. – Programmer Jul 30 '17 at 14:08
  • @Programmer Yeah I get it, I see a lot of issues too, like how to handle child/parent relationships etc. – Green_qaue Jul 30 '17 at 14:49
  • You can solve that by giving each Object a unique id. Maybe the id starts from 0 and increments up. having a class that stores the parent of child of each object. The parent and child variables will be an `int` which has the unique id of each child and parent object. – Programmer Jul 30 '17 at 14:52
  • 1
    Wonder how to get all components from one object? use `GameObject.GetComponents(typeof(Component)`...Find Child GameObjects? `Transform.GetChild(i)` in a loop. Find all Object's in the scene? use `Resources.FindObjectsOfTypeAll()`. This should get you started. – Programmer Jul 30 '17 at 15:04
  • @Programmer Thanks for your help, it was enough to point me in the right direction. Now have a nice little Event-System that handles everything. And saving parents/children worked our in the end :) – Green_qaue Aug 01 '17 at 22:06
  • 2
    Nice and remember not to save a Texture2D. Implement a way to save the path instead or your save file will be huge. – Programmer Aug 02 '17 at 00:36
  • @Programmer If a prefab has more than 1 Texture I always cache them in a Sprite-array in the Prefab object. So that will never be an issue :) – Green_qaue Aug 02 '17 at 08:44

0 Answers0