Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Keeping track of multiple gameobjects being destroyed and instantiated in JSON?

Discussion in 'Scripting' started by Lethn, Mar 22, 2022.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,584


    I've come across this tutorial that was recently uploaded and I was quite impressed actually at how in depth this is and it's one of the better tutorials on saving and loading I've seen. Let me know if you guys would do anything differently here or if there are other methods you suggest. I noticed that one of the suggestions he puts forward for keep tracking of whether coins are inactive or not is by simply disabling the renderer.

    This isn't ideal for me is I'd like to be able to destroy the gameobject entirely and keep using my normal code without having to change too much. How would I keep track of this? Let's say I instantiate 5 cubes for example with rigidbodies and I'm keeping track of the vector3 positions, I'm not sure how I would store them at the start based on this code. Hopefully I'm not misinterpreting his explanation too much in the video as I do struggle at times understanding serialization. He seems to be keeping the gameobjects completely active but hidden in order to keep track of them and sending a boolean to each coin to indicate if they have been collected or not. I feel like this method is definitely going to generate too much junk in my game and I would like to be able to completely destroy my gameobjects to prevent problems.

    I am definitely going to be practicing this on a fresh project before I mess with my main game to make sure I don't do anything dumb. I'll have an experiment and see what I can do to keep track of it all.
     
    Last edited: Mar 22, 2022
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    I can't really watch the video, but from what you've said, I can definitely say you shouldn't spawn all objects and then have each one of them decide if they want to be active or not.

    You should have a top-level spawner keeping track of all spawned objects. You'll then just keep track of spawned cubes and re-spawn them when the level loads. Destroyed cubes simply won't be saved in the JSON and that's it.
     
    Lethn likes this.
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,584
    Thank you that cleared things up a bit and of course looking up some of the keywords you're talking about I have a better understanding of what WriteAllText does. If I want a quicksave function for example I'll just need to create a save file with a specific string name and use WriteAllText to overwrite it any time I want the destroyed cubes to stay destroyed in runtime.

    Edit: AHA! Just as an extra note for myself and others I found on search what I was looking for with regards to saving the vector3's of gameobjects. Use foreach loop to save each item to the text file, oh it's nice when I start properly understanding new things and make progress.
     
    Last edited: Mar 23, 2022
  4. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Try looking into JSON libraries. Newtonsoft.Json is an example. With that, you'll be able to just have a
    List<Vector3>
    of positions for your object and to convert it into a properly formatted JSON with just
    Json.Serialize
    after which you just save that string into a file somewhere and that's half the job done.
     
    Lethn likes this.
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,584
    Thanks, is it a well maintained or at least long lasting library? I tend to rely on out of the box stuff for that reason, I'll check it out if it simplifies the code though and still works.
     
    Last edited: Mar 25, 2022