Save and Loading with Ink Mid-Dialogue

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
jspro
Posts: 10
Joined: Tue Nov 24, 2020 5:33 am

Save and Loading with Ink Mid-Dialogue

Post by jspro »

Hello Toni,

I'm trying to setup a save-load system for my project, and would appreciate some help!

Relevant Project Details:
  • I'm using Ink with the DS Ink Integration scripts.
  • I have a "persistent" scene that contains some universal game logic scripts. This scene is only loaded once, and is never unloaded. The Dialogue Manager starts in this scene.
  • I have a pause menu that lets the player save and load.
  • Scene switching and loading is handled purely though ink; some lines are commands that trigger actions, of which scene loading is one of them.
Requirements:
  • To be able to save and load mid-dialogue. Loading mid-dialogue should bring the player back to the same line of dialogue, and load the scene that the player was in.
So far, saving and loading allows the player to save the state of the json file, but not what the player is currently doing. I've found this link (https://www.pixelcrushers.com/phpbb/vie ... ogue#p1971), but I'm not entirely sure if I can use the approach described there, because of how the Dialogue System processes ink files.

Best,
Malek
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save and Loading with Ink Mid-Dialogue

Post by Tony Li »

Hi,

If you set up the Dialogue System's save system and add an InkSaver component to the Dialogue Manager, it should save and restore the current mid-dialogue state.
jspro
Posts: 10
Joined: Tue Nov 24, 2020 5:33 am

Re: Save and Loading with Ink Mid-Dialogue

Post by jspro »

I took another look and re-enabled the "Save Current Scene" boolean on the Save System Component. It turns out it was always saving the "Persistents" scene because while I'm additively loading scenes, the "Persistents" was always the "active" scene. I fixed that by setting newly loaded scenes to be "active".

This poses a new problem for me, however; it's now saving the correct scene, but it's only loading that scene; the "Persistents" scene is not getting loaded. I think I can get it to work by making all the scripts singletons that are not destroyed, but that might be a pain, haha. Is there a better way of doing this?

Best,
Malek
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save and Loading with Ink Mid-Dialogue

Post by Tony Li »

Hi,

Since you're using additive scenes, I recommend UNticking "Save Current Scene" and handling scene loads yourself. Every project that uses additive scenes does it differently, so it's best if you handle it yourself since you know what you want to do.

When "Save Current Scene" is unticked, the save system won't load or unload scenes. It will just save and restore the data of Saver components such as InkSaver and DialogueSystemSaver.
jspro
Posts: 10
Joined: Tue Nov 24, 2020 5:33 am

Re: Save and Loading with Ink Mid-Dialogue

Post by jspro »

If I untick "Save Current Scene", how would you recommend I approach saving/loading the correct scene and integrating that with SaveSystem?

I was considering keeping it ticked but changing the "LoadSceneCoroutine" and LoadScene functions in general to additively load the required scene instead.

Best,
Malek
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save and Loading with Ink Mid-Dialogue

Post by Tony Li »

To avoid having to maintain customizations to SaveSystem.cs, I recommend unticking "Save Current Scene".

Write a saver script that records the current additively-loaded scene(s) and also marks the persistent scene as the current scene to load when loading saved games. Assuming the persistent scene is named "Persistent", there's a rough example: (may have typos)

Code: Select all

public class SceneSaver : Saver
{
    [Serializable]
    public class Data
    {
        public List<string> addedScenes;
    }
    
    public override string RecordData()
    {
        // Update the current saved game data so the "Persistent" scene is the main scene to load:
        SaveSystem.currentSavedGameData.sceneName = "Persistent";
        
        // Then save the list of additively-loaded scenes:
        var data = new Data() { addedScenes = new List<string>(SaveSystem.addedScenes); }
        return SaveSystem.Serialize(data);
    }
    
    public override void ApplyData(string s)
    {
        if (string.IsNullOrEmpty(s)) return;
        var data = SaveSystem.Deserialize<Data>(s);
        if (data == null) return;
        foreach (string sceneName in data.addedScenes)
        {
            SaveSystem.LoadAdditiveScene(sceneName);
        }
}
To load a saved game, just load it normally using the SaveSystem (e.g., SaveSystem.LoadFromSlot(#)).

To change scenes, load the next scene additively and unload the outgoing scene. You can use SaveSystem.sceneLoaded to know when to unload the outgoing scene:

Code: Select all

SaveSystem.sceneLoaded += OnSceneLoaded;
SaveSystem.LoadAdditiveScene(nextSceneName);
...
void OnSceneLoaded()
{
    SaveSystem.sceneLoaded -= OnSceneLoaded;
    SaveSystem.UnloadAdditiveScene(outgoingSceneName);
}
jspro
Posts: 10
Joined: Tue Nov 24, 2020 5:33 am

Re: Save and Loading with Ink Mid-Dialogue

Post by jspro »

This looks like it'll work for me. Thanks a lot, Toni!

Best,
Malek
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Save and Loading with Ink Mid-Dialogue

Post by Tony Li »

Glad to help! If you run into any issues, let me know.
Post Reply