Saving /loading ????

Hi guys
So I thought I would get some clue as to how this works. The game I am attempting is a sim/rpg type. There will be a lot of npc’s living there lives so how do i save all this info to be loaded later?.I have quests status(active,failed,current,completed),npc locations.,actions, status(alive.dead,quest available,relationship ).I Objects.traps status(active/deactivated),doors status (unlocked,damaged,locked),treasure chest status etc…Player lvl gold, skills ,Location, relationships to other npcs. Player companions(party) there skills lvl gold status alive dead?etc…There may be more thats just off the top of my head.

One thought I had regarding the quests. Maybe say there are 100 quests you have a string of 100 characters… A=active. F=failed.D=Dormant.S=Solved.Also if a quest is active it would have a timer so maybe instead of a (a)active have a number representing time remaining.Then you can save this string?? I don’t know is that a way to go about it. Quests would be the least complicated I think to save the rest is daunting…

.

You could save the data in a JSON table and just save that to a file
http://wiki.unity3d.com/index.php/SimpleJSON
https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

Maybe have different files for player data (inventory, health, etc), quests, enemies, etc.

are you asking what platform you should use e.g. database/text/xml or what coding structure? To me it seems like a db might be the preferred option for you. if not, you will have to write a xml parser that can save, extract and populate the data to the npc objects.

Sorry, Diericx beat me:)

Consider stepping back for a moment to get an idea of the big picture.

In all likelihood, a game like this will be bigger than a single scene, so objects and NPCs will page in and out. Before an object (including NPCs) disappears, it will need to save its state. When it reappears, it’ll need to restore its state. So you need to be able to:

  • Page objects in and out of the scene, and

  • Save and restore object state.

When and how to page is its own topic; I’ll omit it from this reply.

You’ll want some kind of database to hold the state information. I use “database” loosely; any collection of data is technically a database. Make it a random-access database so objects can find and update their records without having to deal with the rest of the data in the database. If you can fit the entire world’s state in an in-memory database, that’s the fastest way to go. Maybe use an in-memory SQLite database, or a custom data structure, or whatever.

Your objects should register with some kind of state manager. When they appear, they can restore their state from the database. From they disappear, they can save their state to the database (and then deregister from the state manager). The state manager should also be able to tell all in-scene objects to save their state immediately to the database. You’ll use this when saving your game.

You can also have one or more invisible global “objects” that track things like quest states, what zone the player is in, etc. They’ll also use the database to save and restore this information.

When you save, tell all in-scene objects to save their state to the database. Then serialize the database. Again, I use “serialize” loosely; it just means put the data into a sequential format. It doesn’t matter whether this format is JSON, XML, etc. Then write this out to permanent storage – a local disk file, PlayerPrefs, a cloud service, whatever.

When you load, deserialize the database (i.e., load it). Then you can offload the rest of the load process to the objects in the scene. When you start, the scene will probably just have your global objects. Say one of these keeps track of which zone the player was in when the game was saved. This object can then load that zone, place the player in it, and load in all the objects that were there at the time. When the objects are added to the zone, they can in turn retrieve their state from the database.

So you can see that there’s quite a bit to plan out before you get into the weeds of whether to use strings or bit flags or whatever in the serialized format. It’ll save you a lot of headaches to get this straight from the get-go. Good luck!

1 Like

Hey Tony just had a quick read of what you wrote don’t understand much of it will have to do some research to try grasp it I guess.
“When you save, tell all in-scene objects to save their state to the database”
This line caught most of my attention how would you tell them to save there states?

“Your objects should register with some kind of state manager. When they appear, they can restore their state from the database. From they disappear, they can save their state to the database (and then deregister from the state” manager)
If a npc isn’t in scene or viewed by my character he will still be going about his life so would this apply then the calculations for npc’s continues wheher they are near the player or not .

Curious can a mecanim animation be saved midway and played back upon load at the point it was saved?Say you save game while someone is swinging a weapon etc you would want the load to continue where the swing left off.

From a very high level view, each object will register itself with a manager. The manager has a list of registered objects. It can invoke a “save your state” method on each object. There are a lot of sophisticated features in C# and Unity that you could use to do this. Here’s a very simple example using a minimum of language features:

public class DataObjectManager : MonoBehaviour {
    private List<DataObject> dataObjects = new List<DataObject>();
   
    public void Register(DataObject dataObject) {
        dataObjects.Add(dataObject);
    }

    public void Unregister(DataObject dataObject) {
        dataObjects.Remove(dataObject);
    }

    public void SaveAllData() {
        foreach (var dataObject in dataObjects) {
            dataObject.SaveYourself();
        }
    }
}

//=================================
public class DataObject : MonoBehaviour {
    private DataObjectManager dataObjectManager;

    void Start() {
        dataObjectManager = FindObjectOfType<DataObjectManager>();
        dataObjectManager.Register(this);
        RestoreYourself();
    }

    void OnDestroy() {
        dataObjectManager.Unregister(this);
    }

    public virtual void SaveYourself() {
        // Override with your code to save current state to the database.
    }

    public virtual void RestoreYourself() {
        // Override with your code to restore state from the database.
    }

Again, this is really simplified example code. It doesn’t do any error checking, and it doesn’t use interesting C# features like interfaces or events, nor does it use Unity features like UnityEvents. I just wrote it to convey the idea without that extra junk.

In that case, they’ll never disappear, so they always stay registered and ready to receive SaveYourself and RestoreYourself method invocations. In practice, though, at the very least you’ll switch faraway NPCs to a simpler AI routine because you don’t want to eat up CPU doing detailed simulation of NPCs that the player can’t see.

Not easily at all. But you won’t find many games that save that level of detail anyway. Just save the NPC’s “mental state” (e.g., “attacking the player”) and let the rest start up as the game plays.

What I really want to achieve in the end is something like the Unity editor pause I know its not technically a save function but it must be storing all the data? I mean you can leave the thing paused for hours and it will continue exactly where is was paused from. including mecanim states

There are some good scene serialization plugins on the Asset Store. I think some are free even. You might want to look into those.

1 Like

Will do thx Is it possible to get most of the game done then only start arranging the save/load stuff or is that not really a option?I would like to get the game first to a showcase level and save/load is not exactly a showcase feature

Sounds reasonable. Find the fun first (i.e., the core gameplay loop). You’re not going to dig yourself in too deep a hole by putting off saving and loading for later. As a general tip, try to keep the individual systems (inventory, player control, world event triggers, etc.) as separate and independent as possible in design. (“Decoupled” in programming parlance.) This way you’ll minimize the amount of things you may need revamp as you implement saving and loading. Best of luck!

Ron Gilbert (of classic adventure game fame) posted a great article about saving games in their upcoming game, Thumbleweed Park. The whole article is well worth reading. The gist is:

He explains how he implemented it in Thimbleweed Park.

1 Like

Well I agree with the article in that saving/load is a scary monster. It’s something taken for granted in games much like one takes your skeleton for granted but without one or if a bone gets broken then it really hurts you bad.

1 Like