Lua Variables Persistence?

Announcements, support questions, and discussion for the Dialogue System.
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post by VoodooDetective »

Actually, couldn't I just use SaveSystem.loadEnded for that, or is that different?

Edit:
Ah, just tested it. Seems like that's different. So now I'm not sure the best way to deal with this. So creating an event that's called in a subclass of Dialogue System Saver when applyData completes is one idea, but that won't be called unless you scene change into that scene.

That's not always the case so I can't depend on that for post-dialogue system-init code. Is there a better way to hook into the dialogue system lifecycle? I really just want to be able to have code run in objects after DS is initialized (ie. I want to look at my variables, etc.).
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua Variables Persistence?

Post by Tony Li »

Hi,

This patch for SaveSystem includes a new event that you can hook into: SaveSystem.saveDataApplied. It's called after ApplyData() has been called on all savers in the newly-loaded scene:

DS_SaveSystemPatch_2020-04-22.unitypackage

(Compare this to SaveSystem.loadEnded, which as you mentioned is called after the scene is loaded, but not after the save data has been applied to the newly-loaded scene.)
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post by VoodooDetective »

Oh awesome! Thanks for sharing that! Trying it out right now.

Just to be clear, this event always happens before SceneLoaded if the frames to wait is set to 0?
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua Variables Persistence?

Post by Tony Li »

The sceneLoaded event always happens first. Then after the scene is loaded, the saved game data is applied and then the saveDataApplied event happens.
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post by VoodooDetective »

Is there an event I can handle that tells you when the dialogue system has been fully initialized and is safe to use?

I realized saveDataApplied will only work when you transition into a new scene. It won't fire when you start the game on a scene. For instance, how do you know when it's safe to use DialogueSystem on the first scene of your game? Is there an initialization callback after DS is instantiated?

Ahh, could I use a combination of:
DialogueSystemController.initializationComplete and saveDataApplied to get an event that represents DS has fully initialized be it from a transition or from an initial instantiation? Ah but that becomes messy when you're trying to figure out when to register a handler for that event because DS may not exist yet. I guess it would work if DS was not dynamically instantiated. Maybe that's what I need to do.

OK a summary:
Have DS exist in scene before
Create a static event representing initialization of DS
register to initializationComplete in awake, also register to saveDataApplied in awake.
Fire the static event if either one fires.

anyone who wants to know when the dialogue system has been initialized can listen to that static event. Does that sound reasonable?
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua Variables Persistence?

Post by Tony Li »

Hi,

The short way is to always wait the number of frames it would take to apply data:

Code: Select all

IEnumerator Start()
{
    for (int i = 0; i <= SaveSystem.framesToWaitBeforeApplyData; i++)
    {
         yield return null;
    }
    Debug.Log("READY!");
}
Although if you're not loading a saved game or changing scenes (i.e., you're starting play directly in this scene), you'll wait one or more frames when you didn't really need to. But it might be a fine tradeoff for the simpler code.

The long way is to hook into a few events:

Code: Select all

using UnityEngine;
using PixelCrushers;
using System;
using PixelCrushers.DialogueSystem;

public class TestSaveSystemEvents : MonoBehaviour // Best to add to Dialogue Manager.
{
    private bool isChangingScenes = false;

    private void Awake()
    {
        var dialogueSystemController = FindObjectOfType<DialogueSystemController>();
        dialogueSystemController.initializationComplete += OnInitializationComplete;
        SceneNotifier.willUnloadScene += OnSceneWillChange;
        SaveSystem.saveDataApplied += OnSaveDataApplied;
    }

    private void OnInitializationComplete() // Called when Dialogue Manager has initialized.
    {
        if (!isChangingScenes) Debug.Log("READY! (Not changing scenes.)");
    }

    private void OnSceneWillChange(int sceneIndex) // Called before Save System changes scene.
    {
        isChangingScenes = true;
    }

    private void OnSaveDataApplied() // Called after Save System has changed scene and applied data.
    {
        isChangingScenes = false;
        Debug.Log("READY! (After scene change and applying save data.)");
    }
}
VoodooDetective
Posts: 222
Joined: Wed Jan 22, 2020 10:48 pm

Re: Lua Variables Persistence?

Post by VoodooDetective »

Awesome, thanks so much for all the help! So I did a lot more thinking and realized my problem had a lot to do with the order of initialization for my game. I ended up creating a "preload" scene where DS and a number of other objects are created before the game really starts. That way every scene can expect DS to be around, and every scene can expect to have that apply event called.

I'm using this editor script (http://wiki.unity3d.com/index.php/SceneAutoLoader) to always start in the "preload" scene and transition to the desired scene. In the real game I'll have that be or be before my splash screen and hardcode the start scene for the game.

I mention all of this in case anyone else runs into this and is scratching their head about how to handle it.
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Lua Variables Persistence?

Post by Tony Li »

Sounds good. Thanks for sharing your solution!
Post Reply