Creating save data means Dialogue in database stops updating

Announcements, support questions, and discussion for the Dialogue System.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Creating save data means Dialogue in database stops updating

Post by Tony Li »

Hi,

You can add a script that saves just those fields. This example script below hooks into PersistentDataManager.GetCustomSaveData to append Lua code to the saved game data. When loading a saved game, the Lua code will run and restore those fields' values.

SaveCustomFields.cs

Code: Select all

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

public class SaveCustomFields : MonoBehaviour
{
    public string[] fieldsToSave = { "Morality" };

    private void Awake()
    {
        PersistentDataManager.GetCustomSaveData = RecordCustomFields;
    }

    private string RecordCustomFields()
    {
        var sb = new StringBuilder();
        foreach (var quest in DialogueManager.masterDatabase.items)
        {
            if (quest.IsItem) continue;
            foreach (var fieldTitle in fieldsToSave)
            {
                var index = DialogueLua.StringToTableIndex(quest.Name);
                var key = DialogueLua.StringToTableIndex(fieldTitle);
                var value = GetFieldValue(DialogueLua.GetQuestField(quest.Name, fieldTitle));
                sb.AppendFormat("Quest[\"{0}\"].{1}={2}; ", index, key, value);
            }
        }
        Debug.Log("Saving extra data: " + sb.ToString());
        return sb.ToString();
    }

    private string GetFieldValue(Lua.Result value)
    {
        if (value.Equals(Lua.noResult) || !value.hasReturnValue) return "nil";
        return value.isString ? ("\"" + value.asString + "\"") : value.asString;
    }
}
[EDIT: Fixed to get current Lua value, not database value.]
bluebuttongames
Posts: 91
Joined: Tue Jul 14, 2015 8:22 am

Re: Creating save data means Dialogue in database stops updating

Post by bluebuttongames »

Ah, great. Thanks.
bluebuttongames
Posts: 91
Joined: Tue Jul 14, 2015 8:22 am

Re: Creating save data means Dialogue in database stops updating

Post by bluebuttongames »

Do you have C# version of that code please?
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Creating save data means Dialogue in database stops updating

Post by Tony Li »

That is C#.
bluebuttongames
Posts: 91
Joined: Tue Jul 14, 2015 8:22 am

Re: Creating save data means Dialogue in database stops updating

Post by bluebuttongames »

Lord.

I need more coffee. Sorry.
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Creating save data means Dialogue in database stops updating

Post by Tony Li »

No problem! :-) I did mention Lua in there, so that might've thrown you.
bluebuttongames
Posts: 91
Joined: Tue Jul 14, 2015 8:22 am

Re: Creating save data means Dialogue in database stops updating

Post by bluebuttongames »

Is there something special I need to do to assign/save these values?

I've attached your script and those fields are being saved into the save data but they are the defaults, not what I've assigned.

I'm using to set:
DialogueLua.SetQuestField(questName,"DayCompleted",2);

and then saving:
string dbData = PersistentDataManager.GetSaveData ();
bluebuttongames
Posts: 91
Joined: Tue Jul 14, 2015 8:22 am

Re: Creating save data means Dialogue in database stops updating

Post by bluebuttongames »

Replacing this made it save the correct value, but that save data won't load back in.

var value = GetFieldValue(quest.AssignedField(fieldTitle));

for

var value = DialogueLua.GetQuestField(quest.Name,fieldTitle).AsString;
User avatar
Tony Li
Posts: 22054
Joined: Thu Jul 18, 2013 1:27 pm

Re: Creating save data means Dialogue in database stops updating

Post by Tony Li »

Oops, sorry about that. I fixed the code above. Please check it for the way the value is saved.

When you load a game, does it throw any errors?
bluebuttongames
Posts: 91
Joined: Tue Jul 14, 2015 8:22 am

Re: Creating save data means Dialogue in database stops updating

Post by bluebuttongames »

That has fixed it! For now...

Thanks for the help, once again.
Post Reply