Only Save Variables

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
boz
Posts: 76
Joined: Mon Oct 19, 2020 8:59 pm

Only Save Variables

Post by boz »

Howdy,

I just want to save the variables in my Dialogue Database between loads/scenes.

In the documentation it says:
If you only want to save the state of the dialogue database (e.g., dialogue database variables, quest states, and SimStatus), you can bypass the Save System and use PixelCrushers.DialogueSystem.PersistentDataManager instead:

Code: Select all

string s = PersistentDataManager.GetSaveData(); // Save state.
PersistentDataManager.ApplySaveData(s); // Restore state.
DialogueManager.ResetDatabase(DatabaseResetOptions.KeepAllLoaded); // Reset state.
Is there more to it than this?

In a script attached to the Dialogue Manager I put:

Code: Select all

    string s;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.S))
        {
            s = PersistentDataManager.GetSaveData(); // Save state.
        }

        if (Input.GetKeyDown(KeyCode.C))
        {
            PersistentDataManager.ApplySaveData(s); // Restore state.
        }
    }
When I test this, the variables still seem to be unaffected by the save/load.

I feel like I'm missing something here.

Thanks for any help :D
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Only Save Variables

Post by Tony Li »

Hi,

That should work. Are you changing any variable value between press 'S' and 'C'?

Are there any errors or warnings in the Console?
boz
Posts: 76
Joined: Mon Oct 19, 2020 8:59 pm

Re: Only Save Variables

Post by boz »

You know what? I just checked the dialogue variable I was testing and it was being set via my script rather than by the dialogue system, so it would've been reset every time.

Good to know it should work, though! I'll do a better test.

This is a load off, thanks :)
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Only Save Variables

Post by Tony Li »

Glad you got to the bottom of it!
boz
Posts: 76
Joined: Mon Oct 19, 2020 8:59 pm

Re: Only Save Variables

Post by boz »

So I was just testing and it did work if I want to return the variable to a previous state, but it didn't work for my intended use-case, which is to be able to return all the variables to their saved state when you reload the game. Does this not work for that use-case? Or am I messing it up still?
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Only Save Variables

Post by Tony Li »

PersistentDataManager.ApplySavedGameData(s) should restore the Lua Variable[] array to the state that was recorded by PersistentDataManager.GetSavedGameData().

What's happening instead?

Are there any errors or warnings in the Console window?
boz
Posts: 76
Joined: Mon Oct 19, 2020 8:59 pm

Re: Only Save Variables

Post by boz »

I finally got it, but I don't know if I did it right.

I noticed that it was saving all the data to a string variable which was getting erased every time I restarted the game, so I had my own save system save that string and then feed it back in and all was well.

I guess I was hoping it would magically take care of all of that for me :oops:
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Only Save Variables

Post by Tony Li »

Hi,

The full save system would take care of it. But since you're only using PersistentDataManager.GetSaveData() and ApplySaveData(), it simply returns a string (which you must save on your own) and restores the variables from the string (which you must load and provide).
Post Reply