Search Unity

Save everything in a scene?

Discussion in 'Scripting' started by Divider_Games, Jun 6, 2019.

  1. Divider_Games

    Divider_Games

    Joined:
    Oct 29, 2015
    Posts:
    8
    I'm in need of some help here as I'm not sure how to do this. When I'm finished with the game I'm making, my scene will have hundreds, if not thousands of objects that you can interact with and then the state they're in (including position, rotation, custom script values and everything else) will need to be saved so the player can exit the game without losing progress.

    I know about serialization and saving to a file, but that would mean that I'd need to create a huge script and save each and every single object as a separate variable, which is simply impractical and adds thousands of lines of (mostly) repetetive code which I will have to type by hand in an otherwise indie project. Is there not a simpler way to do this?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Look for JsonUtility.
     
  3. Divider_Games

    Divider_Games

    Joined:
    Oct 29, 2015
    Posts:
    8
    From what I see this does not fix my problem.

    For example, if an object has 3 scripts attached, each of those scripts has 20 variables and I have 1500 of said objects, that would mean I'd have to manually, by hand, add 90000 (3*20*1500) variables to my save script that would then get saved in json format. There must be a way to avoid that.
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,779
    Nope. You create list of objects (classes).
    So when you have 10 properties per object, multiplying by 100k, that is still only 10 properties to write into save/load script manager. Or whatever you call it. If you need write 90k properties by hand, you got issue with your own game design.

    jsonUtility saves serializable classes. That includes List, but not dictionary.

    Also, since you think you may need save 90k objects, there is a chance, you have bad design. Unless you write something like Minecraft, or tile based map, with items. But that is just my hypothesis.
     
    SparrowGS and Joe-Censored like this.
  5. Divider_Games

    Divider_Games

    Joined:
    Oct 29, 2015
    Posts:
    8

    Hmm, this does seem like what I need then. Is there a good video on this? For example, if I want to save an NPC's position, rotation, scale, current health and mana (both are just simple floats), but I want to do this for all (approximately) 1500 enemies in the game, how would I go about doing that?
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're getting yourself wrapped up on the number of objects. That is not particularly important. In your last example, you can do that with just a simple CSV file on a single line - it is not much information per object. If I were doing this without JsonUtility, I'd just create a single method on a script on your NPC GameObject which serializes all the necessary information to recreate the object.

    Once I had that done, then I'd just loop through all of your NPC's, just calling the same method on each, and dump everything to a file.

    When you load the save, you just go and recreate them all one by one, looping through all the records in the save until you hit the end of the save file. Easy
     
  7. Divider_Games

    Divider_Games

    Joined:
    Oct 29, 2015
    Posts:
    8

    Thank you so much for this! I figured it out thanks to this reply. Respect!
     
    Joe-Censored likes this.