Page 1 of 1

Save States for Time Loops

Posted: Tue Nov 23, 2021 1:16 pm
by eeeli
Hi Tony,

I have a question on saving/variables. I've dug into the Dialogue System's saves a little bit already, but I wanted to ask your advice before implementing a full system to minimize the chances of my messing it up.

The game I'm working on is a time loop, and as such I'm going to have a fair number of world state variables I need to keep track of between loops, but there are also some variables that I'd like to reset each loop. I was wondering what the best way for me to handle this is.

For some additional context, the game exists in one scene, and the loop is 'reset' by reloading that scene. However, because of the way the Dialogue Manager isn't destroyed between scenes none of the variables are being reset. Currently I'm thinking it might be best to have a script that, at the start of each loop, goes through and manually sets all the variables that I want to be reset at the start of loops to their starting values.

I was wondering if you thought that was a decent implementation, or if there's an easier/better way to flag a whole set of variables to be reset.

Thanks so much!

Re: Save States for Time Loops

Posted: Tue Nov 23, 2021 2:04 pm
by Tony Li
Hi,

That would work. Alternatively, you could put a unique code in the variables' Descriptions in the Dialogue Editor's Variables section. For example:

resetVariables.png
resetVariables.png (19.68 KiB) Viewed 140 times

I added the string "[reset]" to variables that should reset at the beginning of each time loop -- that is, when the scene starts. Then you can add a script that resets them:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class ResetVariables : MonoBehaviour
{
    void Start()
    {
        foreach (Variable variable in DialogueManager.masterDatabase.variables)
        {
            if (variable.Description.Contains("[reset]"))
            {
                switch (variable.Type)
                {
                    case FieldType.Boolean:
                        DialogueLua.SetVariable(variable.Name, variable.InitialBoolValue);
                        break;
                    case FieldType.Number:
                        DialogueLua.SetVariable(variable.Name, variable.InitialFloatValue);
                        break;
                    default:
                        DialogueLua.SetVariable(variable.Name, variable.InitialValue);
                        break;
                }
            }
        }
    }
}