Re: Creating save data means Dialogue in database stops updating
Posted: Sun May 03, 2020 11:10 am
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
[EDIT: Fixed to get current Lua value, not database value.]
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;
}
}