Re: Saving additional data from dialog entry field
Posted: Fri Dec 29, 2017 1:07 pm
Glad to help!
Support and discussion forum for Pixel Crushers products
https://www.pixelcrushers.com:443/phpbb/
https://www.pixelcrushers.com:443/phpbb/viewtopic.php?t=1200
Code: Select all
Dictionary<string, DialogueEntry> entriesByArticyId;
void Start()
{
entriesByArticyId = new Dictionary<string, DialogueEntry>();
foreach (var conversation in DialogueManager.MasterDatabase.conversations)
{
foreach (var entry in conversation.dialogueEntries)
{
var articyId = Field.LookupValue(entry.fields, "Articy Id");
entriesByArticyId.Add(articyId, entry);
}
}
}
...
var someEntry = entriesByArticyId[someArticyId];
Code: Select all
foreach (var dialogueEntry in conversation.dialogueEntries)
{
if (Field.LookupBool(dialogueEntry.fields, "IsSequence") || Field.LookupBool(dialogueEntry.fields, "IsLoop"))
{
var currentValue = Field.LookupInt(dialogueEntry.fields, "CurrentValue");
bool isLoop = Field.LookupBool(dialogueEntry.fields, "IsLoop");
bool isSequence = Field.LookupBool(dialogueEntry.fields, "IsSequence");
string articyID = Field.LookupValue(dialogueEntry.fields, "Articy Id");
sb.AppendFormat("[{0}] = {{ CurrentValue = {1} , IsLoop = '{2}' , IsSequence = '{3}', ArticyId = '{4}' }}, ",
dialogueEntry.id, currentValue, isLoop, isSequence, articyID);
// Add affected entry to dialog entry dictionary
entriesByArticyId.Add(articyID, dialogueEntry);
}
}
Code: Select all
foreach(KeyValuePair<string, DialogueEntry> entry in entriesByArticyId)
{
// Get the values stored in CurrentValue from the Lua environment
sb.AppendFormat(Lua.Run("return Conversation[" + entry.Value.conversationID + "].Dialog[" + entry.Value.id + "].ArticyId").AsString + ";");
sb.AppendFormat(Lua.Run("return Conversation[" + entry.Value.conversationID + "].Dialog[" + entry.Value.id + "].CurrentValue").AsString + ";");
}
Code: Select all
while (i < s.Length)
{
// Get the conversation index in the dialogue database:
string articyID = s[i++];
var value = Tools.StringToInt(s[i++]);
DialogueEntry currentEntry = entriesByArticyId[articyID];
var conversation = conversations[currentEntry.conversationID];
// Conversation ID
if (currentEntry.conversationID != lastConversationId)
{
sb.AppendFormat("Conversation[{0}].Dialog = {{", conversation.id);
lastConversationId = currentEntry.conversationID;
}
// Dialogue Entires
sb.AppendFormat("[{0}] = {{ CurrentValue = {1}, IsLoop = '{2}' , IsSequence = '{3}', ArticyId = '{4}' }}, ",
currentEntry.id, value, Field.LookupBool(currentEntry.fields, "IsLoop"), Field.LookupBool(currentEntry.fields, "IsSequence"),
Field.LookupValue(currentEntry.fields, "Articy Id"));
}
Code: Select all
while (i < s.Length)
{
// Get the conversation index in the dialogue database:
string articyID = s[i++];
var value = Tools.StringToInt(s[i++]);
DialogueEntry currentEntry = entriesByArticyId[articyID];
sb.Append("Conversation[" + currentEntry.conversationID + "].Dialog[" + currentEntry.id + "].CurrentValue = " + value);
}
Code: Select all
using UnityEngine;
using System.Collections;
using System.Text;
using PixelCrushers.DialogueSystem;
using System.Collections.Generic;
public class SaveExtraField : MonoBehaviour
{
public bool debug;
private Dictionary<string, DialogueEntry> entriesByArticyId;
public void RunStartFromOutside()
{
StartCoroutine (Start ());
}
private IEnumerator Start()
{
// Let Dialogue Manager load database first:
yield return new WaitForEndOfFrame();
// Then add in the custom CurrentValue fields to the Lua environment:
AddCurrentValueFieldsToLua();
// Register a Lua function to parse a string that compactly
// records CurrentValue values:
Lua.RegisterFunction("RestoreCurrentValues", this, SymbolExtensions.GetMethodInfo(() => RestoreCurrentValues(string.Empty)));
// Tell PersistentDataManager to add our custom save data,
// which will be a call to our custom Lua function RestoreCurrentValues,
// passing in a compact record of CurrentValue values.
PersistentDataManager.GetCustomSaveData = GetMyCustomSaveData;
}
private void AddCurrentValueFieldsToLua()
{
// Add CurrentValue fields to the runtime Lua environment in the form
// Conversation[#].Dialog[#].CurrentValue.
// Prepare dictionary
entriesByArticyId = new Dictionary<string, DialogueEntry>();
foreach (var conversation in DialogueManager.MasterDatabase.conversations)
{
var sb = new StringBuilder();
sb.AppendFormat("Conversation[{0}].Dialog = {{", conversation.id);
foreach (var dialogueEntry in conversation.dialogueEntries)
{
if (Field.LookupBool(dialogueEntry.fields, "IsSequence") || Field.LookupBool(dialogueEntry.fields, "IsLoop"))
{
var currentValue = Field.LookupInt(dialogueEntry.fields, "CurrentValue");
bool isLoop = Field.LookupBool(dialogueEntry.fields, "IsLoop");
bool isSequence = Field.LookupBool(dialogueEntry.fields, "IsSequence");
string articyID = Field.LookupValue(dialogueEntry.fields, "Articy Id");
sb.AppendFormat("[{0}] = {{ CurrentValue = {1} , IsLoop = '{2}' , IsSequence = '{3}', ArticyId = '{4}' }}, ",
dialogueEntry.id, currentValue, isLoop, isSequence, articyID);
// Add affected entry to dialog entry dictionary
entriesByArticyId.Add(articyID, dialogueEntry);
//Debug.Log("added id " + articyID + ", dialogue conv id: " + dialogueEntry.conversationID + ", d id: " + dialogueEntry.id);
}
}
sb.Append("}; ");
Lua.Run(sb.ToString(), debug);
}
}
private string GetMyCustomSaveData()
{
// Return a string that calls our custom Lua function, passing it the data
// necessary to set the CurrentValue fields of specific dialogue entries.
var sb = new StringBuilder();
sb.Append("RestoreCurrentValues(\"");
foreach(KeyValuePair<string, DialogueEntry> entry in entriesByArticyId)
{
// Get the values stored in CurrentValue from the Lua environment
sb.AppendFormat(Lua.Run("return Conversation[" + entry.Value.conversationID + "].Dialog[" + entry.Value.id + "].ArticyId").AsString + ";");
sb.AppendFormat(Lua.Run("return Conversation[" + entry.Value.conversationID + "].Dialog[" + entry.Value.id + "].CurrentValue").AsString + ";");
}
sb.Append("\")");
if (debug)
Debug.Log("GetMyCustomSaveData: " + sb);
return sb.ToString();
}
private void RestoreCurrentValues(string data)
{
if (string.IsNullOrEmpty(data))
return;
var conversations = DialogueManager.MasterDatabase.conversations;
string[] s = data.Split(';');
var i = 0;
int lastConversationId = -1;
// Start a Lua command that will set the conversation's CurrentValue fields:
var sb = new StringBuilder();
while (i < s.Length)
{
// Get the conversation index in the dialogue database:
string articyID = s[i++];
var value = Tools.StringToInt(s[i++]);
DialogueEntry currentEntry = entriesByArticyId[articyID];
// Version 1:
var conversation = conversations[currentEntry.conversationID];
// Conversation ID
if (currentEntry.conversationID != lastConversationId)
{
sb.AppendFormat("Conversation[{0}].Dialog = {{", conversation.id);
lastConversationId = currentEntry.conversationID;
}
// Dialogue Entires
sb.AppendFormat("[{0}] = {{ CurrentValue = {1}, IsLoop = '{2}' , IsSequence = '{3}', ArticyId = '{4}' }}, ",
currentEntry.id, value, Field.LookupBool(currentEntry.fields, "IsLoop"), Field.LookupBool(currentEntry.fields, "IsSequence"),
Field.LookupValue(currentEntry.fields, "Articy Id"));
// Version 2: Shouldn't this be enough?
// sb.Append("Conversation[" + currentEntry.conversationID + "].Dialog[" + currentEntry.id + "].CurrentValue = " + value);
}
/*
* ORIGINAL WHILE LOOP:
while (i < s.Length)
{
// Get the conversation index in the dialogue database:
var c = Tools.StringToInt(s[i++]);
if (!(0 <= c && c < conversations.Count)) return; // Sanity check.
var conversation = conversations[c];
// Start a Lua command that will set the conversation's CurrentValue fields:
var sb = new StringBuilder();
sb.AppendFormat("Conversation[{0}].Dialog = {{", conversation.id);
// Process all recorded dialogue entries' CurrentValues:
var done = false;
while (!done && i < s.Length)
{
var d = Tools.StringToInt(s[i++]);
if (d == -1) // Our special signifier that this conversation's list of values is done.
{
done = true;
}
else
{
var dialogueEntry = conversation.dialogueEntries[d];
var currentValue = Tools.StringToInt(s[i++]);
sb.AppendFormat("[{0}] = {{ CurrentValue = {1}, IsLoop = '{2}' , IsSequence = '{3}', ArticyId = '{4}' }}, ",
dialogueEntry.id, currentValue, Field.LookupBool(dialogueEntry.fields, "IsLoop"), Field.LookupBool(dialogueEntry.fields, "IsSequence"),
Field.LookupValue(dialogueEntry.fields, "Articy Id"));
//entriesByArticyId[Field.LookupValue(dialogueEntry.fields, "Articy Id")].id
}
}
*/
// Run the Lua command to set the conversation's CurrentValue fields:
sb.Append("}; ");
Lua.Run(sb.ToString(), debug);
}
}
Code: Select all
while (i < s.Length)
Code: Select all
while ((i + 1) < s.Length)