Page 2 of 2

Re: Save System Between Scenes

Posted: Wed Jan 12, 2022 3:47 pm
by xyztankman
Awesome, just put it together on how it could work.

Thanks Tony, I'll probably be back with more questions eventually!

Re: Save System Between Scenes

Posted: Wed Jan 12, 2022 3:50 pm
by Tony Li
Sounds good!

Re: Save System Between Scenes

Posted: Thu Jan 20, 2022 3:22 pm
by xyztankman
Hey Tony, pretty much got this working now, only one last hangup.

I have the Dialogue Manager as a prefab now that survives scene changes, but when the scenes change it looks like my buttons lose reference to the scripts on the Dialogue Manager.

I have the prefab in all scenes, and it has the same setup as in the DemoScenes.

I also have my Audio Manager set up the same way and that is working (not losing the reference) but my script for that is a bit different in how it is instanced.

Code: Select all

private void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else if (instance != this)
        {
            Destroy(instance.gameObject);
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
    }
I found in the SaveSystem.cs script that it is set up as below:

Code: Select all

private void Awake()
        {
            if (m_instance == null)
            {
                m_instance = this;
                if (transform.parent != null) transform.SetParent(null);
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }

Re: Save System Between Scenes

Posted: Thu Jan 20, 2022 4:02 pm
by Tony Li
Hi,

To maintain a reference to the Dialogue Manager, your button must be in the Dialogue Manager's hierarchy or set to Don't Destroy On Load in the same scene in which the Dialogue Manager came into existence.

By way of explanation, let's say you have this setup:

Scene A GameObjects:
  • Dialogue Manager A
  • Button A: points to Dialogue Manager A
Scene B GameObjects:
  • Dialogue Manager B
  • Button B: points to Dialogue Manager B

If you play either of the scene above individually, directly in play mode, they'll work fine.

However, say you start in Scene A. Then you move to Scene B. Now these GameObjects exist:

Active Runtime Scene(s):
  • Dialogue Manager A
  • Button B: points to Dialogue Manager B
Since Button B was configured to point to Dialogue Manager B, it now has a missing reference.

Re: Save System Between Scenes

Posted: Thu Jan 20, 2022 5:10 pm
by xyztankman
If you play either of the scene above individually, directly in play mode, they'll work fine.

However, say you start in Scene A. Then you move to Scene B. Now these GameObjects exist:

Active Runtime Scene(s):
  • Dialogue Manager A
  • Button B: points to Dialogue Manager B
Since Button B was configured to point to Dialogue Manager B, it now has a missing reference.
That makes sense, so I think I would basically have to make my regular canvas into a prefab and set it to Don't Destroy On Load or something along those lines. Ok, I'll give it a go. Going to do a bit more research as well.

Re: Save System Between Scenes

Posted: Thu Jan 20, 2022 7:12 pm
by Tony Li
Or hook up your UI Buttons to script methods that find the current Dialogue Manager using DialogueManager.instance -- or even just use the DialogueManager script methods directly. For example:

Code: Select all

// If this script is on a UI Button, you can configure the Button's OnClick() to call StopCurrentConversation:
public void StopCurrentConversation()
{
    DialogueManager.StopConversation();
}

// Or, if the Dialogue Manager has a custom script of your own that the Button needs to use, configure OnClick():
public void RunMyCustomMethod()
{
    DialogueManager.instance.GetComponent<MyCustomScript>().MyCustomMethod();
}

Re: Save System Between Scenes

Posted: Tue Jan 25, 2022 7:04 pm
by xyztankman
Hey Tony, got it to work by using the script methods! Confirmed that it is saving them to the slots, I also have data from other scripts outside of the Dialogue Manager that I would like to save to the same slot. Is there a direct spot where I can store those variables as well?

Re: Save System Between Scenes

Posted: Tue Jan 25, 2022 7:35 pm
by Tony Li
Hi,

For other data, please see: How To: Write Custom Savers

Re: Save System Between Scenes

Posted: Thu Jan 27, 2022 11:55 am
by xyztankman
Thanks Tony, just what I needed! I'm now set on the save system and it's working great.

Re: Save System Between Scenes

Posted: Thu Jan 27, 2022 2:16 pm
by Tony Li
Awesome! Glad to help.