Page 3 of 4

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 11:10 am
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.]

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 11:18 am
by bluebuttongames
Ah, great. Thanks.

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 11:39 am
by bluebuttongames
Do you have C# version of that code please?

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 11:48 am
by Tony Li
That is C#.

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 2:16 pm
by bluebuttongames
Lord.

I need more coffee. Sorry.

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 2:19 pm
by Tony Li
No problem! :-) I did mention Lua in there, so that might've thrown you.

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 3:19 pm
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 ();

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 4:27 pm
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;

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 4:58 pm
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?

Re: Creating save data means Dialogue in database stops updating

Posted: Sun May 03, 2020 6:58 pm
by bluebuttongames
That has fixed it! For now...

Thanks for the help, once again.