Saving and Loading Mid-Dialogue

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
BaddyDan
Posts: 14
Joined: Tue May 05, 2015 2:52 pm

Saving and Loading Mid-Dialogue

Post by BaddyDan »

Hello,

I am back again with some more questions. Of course, I have searched the documentation as well as the forum, but haven't found anything that can lead me towards my specific inquiry.

Because my game is 100% text based, I was wondering if there is a way to save a game in the middle of a conversation by clicking on a UI Button. And of course, if there is a way to load this saved file, starting the level EXACTLY from the dialogue node in which was saved.

If there is anything you can provide to lead me in the right direction that would be great. Many thanks! :D
User avatar
Tony Li
Posts: 20634
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving and Loading Mid-Dialogue

Post by Tony Li »

Hi,

Yes, but it requires a little bit of scripting or PlayMaker or equivalent.

First, you'll want to keep track of the current dialogue entry node. Add a script like this to the Dialogue Manager:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;

public class RememberCurrentDialogueEntry : MonoBehaviour {

    public void OnConversationStart(Transform actor) {
        var actorName = (DialogueManager.CurrentActor != null) ? DialogueManager.CurrentActor.name : string.Empty;
        var conversantName = (DialogueManager.CurrentConversant != null) ? DialogueManager.CurrentConversant.name : string.Empty;
        DialogueLua.SetVariable("CurrentConversationActor", actorName);
        DialogueLua.SetVariable("CurrentConversationConversant", conversantName);
    }

    public void OnConversationLine(Subtitle subtitle) {
        DialogueLua.SetVariable("CurrentConversationID", subtitle.dialogueEntry.conversationID);
        DialogueLua.SetVariable("CurrentEntryID", subtitle.dialogueEntry.id);
    }
    
    public void OnConversationEnd(Transform actor) {
        DialogueLua.SetVariable("CurrentConversationID", -1);
    }
    
    public void OnApplyPersistentData() {
        var conversationID = DialogueLua.GetVariable("CurrentConversationID").AsInt;
        var entryID = DialogueLua.GetVariable("CurrentEntryID").AsInt;
        if (conversationID >= 0 && entryID > 0) {
            var conversation = DialogueManager.MasterDatabase.GetConversation(conversationID);
            var actorName = DialogueLua.GetVariable("CurrentConversationActor").AsString;
            var conversantName = DialogueLua.GetVariable("CurrentConversationConversant").AsString;
            var actor = GameObject.Find(actorName);
            var conversant = GameObject.Find(conversantName);
            var actorTransform = (actor != null) ? actor.transform : null;
            var conversantTransform = (conversant != null) ? conversant.transform : null;
            DialogueManager.StopConversation();
            DialogueManager.StartConversation(conversation.Title, actorTransform, conversantTransform, entryID);
        }
    }
}
This script does four things:

1. At the start of the conversation, it records the actor and conversant GameObject names.

2. Every line, it records the conversation and dialogue entry node ID.

3. At the end of the conversation, it sets CurrentConversationID to -1 to signify that no conversation is active.

4. When a game is loaded, it checks the recorded information. If a conversation was active, it starts that conversation at the recorded dialogue entry node.


To save, run this code (or the PlayMaker etc. equivalent):

Code: Select all

string saveData = PersistentDataManager.GetSaveData();
then save that string somewhere. For example:

Code: Select all

PlayerPrefs.SetString("savedgame", saveData);
BaddyDan
Posts: 14
Joined: Tue May 05, 2015 2:52 pm

Re: Saving and Loading Mid-Dialogue

Post by BaddyDan »

Hi,

Thank you very much for the information. That first part makes a lot of sense. As for the actual saving part, you have so far assisted using the PersistentDataManager class. Could I also save this using the GameSaver component?
User avatar
Tony Li
Posts: 20634
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving and Loading Mid-Dialogue

Post by Tony Li »

BaddyDan wrote:Thank you very much for the information. That first part makes a lot of sense. As for the actual saving part, you have so far assisted using the PersistentDataManager class. Could I also save this using the GameSaver component?
Sure. Instructions are here. If any questions come up, just let me know.
BaddyDan
Posts: 14
Joined: Tue May 05, 2015 2:52 pm

Re: Saving and Loading Mid-Dialogue

Post by BaddyDan »

Hi,

I am trying to save my game using the Game Saver component, but it does not appear to be working as expected. My work environment is as follows:
1) I have 3 Unity UI Save buttons and 3 Unity UI Load buttons, each with a Game Saver component.
2) Each button is set with the Player Prefs Key as "savedgame" with slots 0, 1, and 2. Function on use to set to save or load, depending on the button, with the starting level the name of the current scene.
3) Each button also has a Lua Trigger component, which triggers On Use.
4) On each button's On Click() function, I've attached the respective button, and call GameSaver.SaveGame(int) or GameSaver.LoadGame(int) respectively.
5) However, when I try the buttons, they do not appear to be working.

I've been trying to go through the documentation as well as the Save Load Example you provided in the Dialogue System package, but am just unable to map everything together. What is wrong with my setup?
User avatar
Tony Li
Posts: 20634
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving and Loading Mid-Dialogue

Post by Tony Li »

I think this example is closer to what you describe: GameSaverExample_2015-12-17.unitypackage

The GameSaver is on the GameObject Canvas > GameSaver Panel. Buttons inside the panel call the GameSaver’s save and load methods with specific slot numbers. Inspect one of the buttons and examine its OnClick block.

The Dialogue Manager also has a script named RememberCurrentDialogueEntry (from above) that saves the player’s place in the current conversation and resumes the conversation at that point when loading games.

The scene is otherwise identical to the Feature Demo scene, except that I also added a Level Manager to the Dialogue Manager. You only need a Level Manager if the player can move between scenes. It takes care of loading the right scene when loading a saved game.

This package is also on the Dialogue System Extras page.
Post Reply