Managing the state of the world

I’ve been working with Unity for a while now and this is the one thing I just can’t seem to grasp.
Take a gigantic game like Skyrim for example, where every NPC is basically its own class with hundreds of lines of code. How in the world do you save all the information about every single NPC?
I mean little things like:

  • Which dialogue options have already been chosen
  • What the NPC should say in various situations
  • How the NPC reacts to the player

The thing is that I really don’t understand the concept of “everything gets deleted when another scene loads”. If I use a Gameobject with DontDestroyOnLoad as a GameManager, it would be nearly impossible to manage the entire game just in that one object. Just having like 100 complex NPCs would bloat that one object so much that I couldn’t possible hope to save everything in just one single file, right?
Furthermore - if I use a single GameManager, that means I would need to “drag” everything from every scene into every other scene. All the information about whether I already chose dialogue option 1 with an NPC from the beginning of the game would be dragged around the rest of the game. This surely can’t be right, can it?

Every time I see this question asked the answer is “use PlayerPrefs” or “use DontDestroyOnLoad”. Yeah, sure, maybe if the only thing you want to save is the player level, HP and so on. But I want to see someone manage the rest of the game just with PlayerPrefs. Are you planning on creating 100.000 variables for that and save them in what’s basically a text file?

When I used RPG Maker I’d just change a switch from off to on. And that was that. The NPC then had that new state for the rest of the game. But in Unity everything gets deleted, so how do you manage things like these?

Unity does not profide a mechanism to manage this “state of the world”. I agree PlayerPrefs is not appropriate for saving and loading game data, that’s more for quickly save small data such as screen resolution or sound volume…

You would need to look for a solution or a framework outside of Unity. Furthermore, there is no generic way to save and load game states, since which data to persist is specific to a game; the game creator is the only one who knows which data should be persistant.

Technically, you could use a database (local or online), serialized data or your own file format to store your game data. Unity profides a platform independant path to store such files, see:

1 Like