Save System Between Scenes

Announcements, support questions, and discussion for the Dialogue System.
xyztankman
Posts: 54
Joined: Mon Jun 21, 2021 7:48 pm

Re: Save System Between Scenes

Post by xyztankman »

Awesome, just put it together on how it could work.

Thanks Tony, I'll probably be back with more questions eventually!
User avatar
Tony Li
Posts: 22154
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save System Between Scenes

Post by Tony Li »

Sounds good!
xyztankman
Posts: 54
Joined: Mon Jun 21, 2021 7:48 pm

Re: Save System Between Scenes

Post 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);
            }
        }
Attachments
Saver4.JPG
Saver4.JPG (79.93 KiB) Viewed 1753 times
ButtonSave.JPG
ButtonSave.JPG (41.8 KiB) Viewed 1753 times
User avatar
Tony Li
Posts: 22154
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save System Between Scenes

Post 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.
xyztankman
Posts: 54
Joined: Mon Jun 21, 2021 7:48 pm

Re: Save System Between Scenes

Post 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.
User avatar
Tony Li
Posts: 22154
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save System Between Scenes

Post 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();
}
xyztankman
Posts: 54
Joined: Mon Jun 21, 2021 7:48 pm

Re: Save System Between Scenes

Post 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?
User avatar
Tony Li
Posts: 22154
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save System Between Scenes

Post by Tony Li »

Hi,

For other data, please see: How To: Write Custom Savers
xyztankman
Posts: 54
Joined: Mon Jun 21, 2021 7:48 pm

Re: Save System Between Scenes

Post by xyztankman »

Thanks Tony, just what I needed! I'm now set on the save system and it's working great.
User avatar
Tony Li
Posts: 22154
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save System Between Scenes

Post by Tony Li »

Awesome! Glad to help.
Post Reply