Search Unity

Auto save system for a simple city building game.

Discussion in 'Game Design' started by Taigama, Sep 28, 2022.

  1. Taigama

    Taigama

    Joined:
    Oct 17, 2015
    Posts:
    27
    Hello.
    I am making a city building game, want to design an auto-save system.

    The game is like this.
    - I have to save both structures and people.
    - The map is large and there are A LOT of people, may be more than 1 million!.
    - People are not all the same. They have their own role, like worker, boss, bartender, kindergartener.
    - People doesn't interact with each other, they only interact with structure (join into the structure). To keep the game simple, I will animate "chit chat sound text effect" over a cafe or a bar instead of having people chatting on the road.
    - People only have 2 state: inside a structure, walking on the street.
    - People can change their role after come in and out from a structure. It is like they graduate from a school after... 1 minute.
    - People density of every place are not the same. There may have roads with only 1 person but other roads with full of people. People stucked on the road will not able to come to the destination on time.
    - We can see people walk on street all the time. No day no night no sleep no rest. Simple routine.

    routine.png

    What I did
    - Save all structures into a list
    - Save all people into another list
    - Serialize all them into a json
    - Save to player pref
    Problem:
    - There are so many "people" so the frame-by-frame saving have a big problem: cost too much time to serialize and save to player pref.

    Any idea for an auto-save system for this game? Thank you very much?
     
    Last edited: Sep 28, 2022
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,956
    Don't save that much data to PlayerPrefs! That system isn't designed to handle megabytes of data.

    No game of this scale implements a "per frame" autosave. Usually games like these will save like every 5 minutes at most and if that means a hiccup for half a second then that's what it is. It shouldn't block the game for 5 seconds or more however.

    As for people, it should not be necessary to save them individually. No player is going to remember which peep was where when the game was saved. So you could just save how many you have, and maybe how many of them were in a specific grid cell of the game world, then spawn them anywhere based on some rules and make them go about their day as if nothing happened.

    Lastly, with so many people going about I assume this is a DOTS game, since regular single-threaded, non-bursted Unity will not be able to handle millions of game objects.
     
    YBtheS, angrypenguin and Taigama like this.
  3. Taigama

    Taigama

    Joined:
    Oct 17, 2015
    Posts:
    27
    Thank you very much! Your sharing is super useful for me!
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    I second all of @CodeSmile's points.

    1. Don't use PlayerPrefs. Instead, learn to use .NET's built-in file IO functionality. You're making JSON already, saving it to a file is far faster and more flexible than using PlayerPrefs, and will only take an hour or two to learn. (Look up C# tutorials for writing text files.)

    2. A periodic auto-save is a decent solution, 99% as good as continuous with a tiny fraction of the overheads. Better yet, only save when something "important" happens, e.g. player places a building, completes an objective, has pressed the save or quit buttons.

    3. Only save important data. The position of every person probably isn't important. Just save data for key assets, e.g. buildings, hero units, quest specific objects. Also, just save enough data for them to re-construct a valid state. As CodeSmile says, this does require some logic (e.g. to guess at decent positions to spawn people when loading) so it needs consideration early in your game design.

    It sounds like you're already getting towards some of these, and if your current system works then just iterate on that. Hopefully these are good pointers for next time you start a similar piece of work, though.
     
    Taigama and YBtheS like this.
  5. Taigama

    Taigama

    Joined:
    Oct 17, 2015
    Posts:
    27
    Thank you very much for your advice. Very clear.