using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class AddResponsesExample : MonoBehaviour
{
public List<GameObject> visitors;
private void Awake()
{
Lua.RegisterFunction(nameof(MakeVisitorList), this, SymbolExtensions.GetMethodInfo(() => MakeVisitorList()));
}
private void MakeVisitorList()
{
Debug.Log("Making visitor list:");
// Get the current entry ID: (This is a special Lua variable in the Dialogue System.)
var entryID = Lua.Run("return thisID").asInt;
// Get the current conversation & entry:
var database = DialogueManager.masterDatabase;
var conversation = database.GetConversation(DialogueManager.lastConversationID);
var entry = database.GetDialogueEntry(conversation.id, entryID);
// Remove old outgoing entries:
foreach (var link in entry.outgoingLinks)
{
var responseEntry = database.GetDialogueEntry(link);
conversation.dialogueEntries.Remove(responseEntry);
}
entry.outgoingLinks.Clear();
// Add new outgoing entries:
var template = Template.FromDefault();
foreach (var visitor in visitors)
{
if (visitor.activeInHierarchy)
{
Debug.Log($"Adding visitor: {visitor.name}");
entry.DialogueText += $"\n{visitor.name}";
var responseEntry = template.CreateDialogueEntry(template.GetNextDialogueEntryID(conversation), conversation.id, visitor.name);
responseEntry.ActorID = entry.ConversantID; // Assign to player.
responseEntry.MenuText = $"Approach {visitor.name}";
responseEntry.DialogueText = $"You approach {visitor.name}.";
conversation.dialogueEntries.Add(responseEntry);
entry.outgoingLinks.Add(new Link(conversation.id, entry.id, conversation.id, responseEntry.id));
}
}
Debug.Log("--end of list--");
}
}
Aaand again i need your help!
I'm trying to send text that is stored in JSON and deserialized into a string. In this text, the places where the name of the city is inserted are marked and each time these places are different. Unfortunately, if I write
"You can sell [em2][lua(GetResourceName())][/em2] in [em1][lua(GetSellSettlementName())][/em1]
then exactly this text appears in the dialog even if I use FormattedText.Parse().
Is it possible to somehow save the lua code in a string and then insert it into the node so that this code is processed?
Is it perhaps the order in which your custom code is running?
As a test, I registered a function GetTestName() and set the first line of Private Hart's conversation in DemoScene1 to:
[em1][lua(GetTestName())][/em1] We need to intercept the launch codes...
and it works correctly. However, it's pulling the text above directly from the database. If you're using an OnConversationLine method to modify the text, you may need to run it through FormattedText.Parse again. Example:
void OnConversationLine(Subtitle subtitle)
{
// Get text from JSON:
string customText = GetTextFromJson();
// Completely replace subtitle text with parsed version of text from JSON:
subtitle.formattedText = FormattedText.Parse(customText);
}
void OnConversationLine(Subtitle subtitle)
{
if (Field.LookupBool(subtitle.dialogueEntry.fields, "Is Right Node"))
{
// Get text from JSON:
string customText = GetTextFromJson();
// Completely replace subtitle text with parsed version of text from JSON:
subtitle.formattedText = FormattedText.Parse(customText);
}
}
It seems I understood.
The ability to customize fields is in Templates –> All Fields?
And if i make field "isRemovable = false" it will autimaticaly add to all nodes in all conversations, except for those that I manually mark as true?