Persistent variables using the Visual Novel framework

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Persistent variables using the Visual Novel framework

Post by lostmushroom »

Hey Tony. Is there a way to have some variables that retain their value across multiple games? The game I'm making using the visual novel framework is meant to be played several times, so some things have to be "remembered" from past games. This also ties in with some achievements (for example the "get every ending" achievement will need to remember what endings players have unlocked in past playthroughs). As it is currently, the variables all get reset at the start of every new game.

One other thing - I disabled the loading and saving, since this game only takes a couple of minutes to play. Just in case that changes things..

Thanks!
User avatar
Tony Li
Posts: 22133
Joined: Thu Jul 18, 2013 1:27 pm

Re: Persistent variables using the Visual Novel framework

Post by Tony Li »

Achievements are saved in PlayerPrefs, independently of saved games, so you're all set there.

You could write a Saver script that saves a list of DS variables to PlayerPrefs, too. Something like this, which you could add to your Dialogue Manager GameObject:

PersistentVariableSaver.cs

Code: Select all

using UnityEngine;
using PixelCrushers;
using PixelCrushers.DialogueSystem;

public class PersistentVariableSaver : Saver
{

    public string[] variables; // Assign in inspector.

    public override string RecordData()
    {
        Debug.Log("PersistentVariableSaver.RecordData:");
        // Record variable values to PlayerPrefs:
        foreach (var variable in variables)
        {
            Debug.Log($"PlayerPrefs(Variable.{variable}) = {DialogueLua.GetVariable(variable).asString}");
            PlayerPrefs.SetString("Variable." + variable, DialogueLua.GetVariable(variable).asString);
        }
        return string.Empty;
    }

    public override void ApplyData(string s)
    {
        Debug.Log("PersistentVariableSaver.ApplyData:");
        // Retrieve variable values from PlayerPrefs:
        foreach (var variable in variables)
        {
            if (PlayerPrefs.HasKey("Variable." + variable))
            {
                var stringValue = PlayerPrefs.GetString("Variable." + variable);
                Debug.Log($"Variable[{variable}] = {stringValue}");
                switch (DialogueManager.masterDatabase.GetVariable(variable).Type)
                {
                    case FieldType.Boolean:
                        DialogueLua.SetVariable(variable, Tools.StringToBool(stringValue));
                        break;
                    case FieldType.Text:
                        DialogueLua.SetVariable(variable, stringValue);
                        break;
                    default:
                        DialogueLua.SetVariable(variable, Tools.StringToFloat(stringValue));
                        break;
                }
            }
        }
    }
}
In the component's Variables list, specify the names of the persistent variables.
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Re: Persistent variables using the Visual Novel framework

Post by lostmushroom »

Hey Tony, thanks for this. I just tried it out, but it doesn't seem to be working - possibly because of this warning about the saver component?
Persistent.JPG
Persistent.JPG (27.05 KiB) Viewed 1154 times
User avatar
Tony Li
Posts: 22133
Joined: Thu Jul 18, 2013 1:27 pm

Re: Persistent variables using the Visual Novel framework

Post by Tony Li »

Hi,

Set unique key values for each saver component. For example, if you have a DialogueSystemSaver and a PersistentVariableSaver, you could set the keys to:

DialogueSystemSaver key: "ds"
PersistentVariableSaver key: "persistentVars"
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Re: Persistent variables using the Visual Novel framework

Post by lostmushroom »

Hey Tony - still no luck. I set unique keys, but it doesn't seem to be remembering the variables. When I quit the game, the CHRName variable returns to being empty, instead of retaining its value.

I checked the console and saw these warnings, although these look related to removing the load and save rather than this new script.

Is there anything else that needs to be done with the unique saver keys other than setting them in the relevant components?
Warnings.JPG
Warnings.JPG (52.38 KiB) Viewed 1148 times
User avatar
Tony Li
Posts: 22133
Joined: Thu Jul 18, 2013 1:27 pm

Re: Persistent variables using the Visual Novel framework

Post by Tony Li »

Hi,

Try adding some Debug.Log lines to the script. I added some suggested Debug.Log lines (4 of them) to the script above. This might give you an idea of what's going on.
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Re: Persistent variables using the Visual Novel framework

Post by lostmushroom »

Thanks for the updated script - I just tried it and the only debug log that shows up is Apply Data.
DebugLog.JPG
DebugLog.JPG (12.46 KiB) Viewed 1142 times
Is it something to do with saved games being empty in the PlayerPrefs component? I'm guessing the reason it's not working is something to do with me messing around with the loading and saving functions.
PlayerPrefs.JPG
PlayerPrefs.JPG (21.89 KiB) Viewed 1142 times
User avatar
Tony Li
Posts: 22133
Joined: Thu Jul 18, 2013 1:27 pm

Re: Persistent variables using the Visual Novel framework

Post by Tony Li »

Hi,

Are you saving the game at any point? If you're not using the Save button (at the top of the basic VN Framework template) / save menu, you could add an Auto Save Load component to your Dialogue Manager. UNtick Load On Start. This component will save the game when the player exits the program. You'll also want to configure the Visual Novel Menu Canvas's QuitToMenuConfirm button to first save the game, then do the other two actions that are in the OnClick() event (return to menu and deactivate QuitToMenuPanel). To configure the button to save the game, add a SaveSystemMethods component to it, and configure OnClick() to call SaveSystemMethods.SaveToSlot.
lostmushroom
Posts: 195
Joined: Mon Jul 01, 2019 1:21 pm

Re: Persistent variables using the Visual Novel framework

Post by lostmushroom »

It's working now with the auto save load - the variables are persistent now when exiting and restarting the game.

Instead of using the quit button, is there a way to configure it to save when the conversation ends? (The whole game is one conversation, so when the conversation finishes, that counts as one game being completed). I removed the quit button since there isn't much use for it in a game this short. Would it be possible to do this by adding an event on the last node of the conversation?
User avatar
Tony Li
Posts: 22133
Joined: Thu Jul 18, 2013 1:27 pm

Re: Persistent variables using the Visual Novel framework

Post by Tony Li »

Hi,

Yup! Add a Dialogue System Events component and Save System Methods component to the Dialogue Manager GameObject. Configure the Dialogue System Events' OnConversationEnd() event to call SaveSystemMethods.SaveToSlot.
Post Reply