Page 5 of 7

Re: Substring replacement

Posted: Wed Nov 02, 2022 3:25 pm
by Tony Li
Here's an example scene:

DS_AddResponsesExample_2022-11-02.unitypackage

It uses this script:

Code: Select all

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--");
    }
}

Re: Substring replacement

Posted: Wed Nov 02, 2022 3:46 pm
by korsak42
Thank you very much! I will try it tomorrow, totally exhausted after trying to beat this problem all day.

Re: Substring replacement

Posted: Thu Nov 03, 2022 8:57 am
by korsak42
OMG, it works! Thank you very much!

Re: Substring replacement

Posted: Thu Nov 03, 2022 9:03 am
by Tony Li
Glad to help!

Re: Substring replacement

Posted: Fri Nov 18, 2022 3:46 am
by korsak42
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

Code: Select all

"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?

Re: Substring replacement

Posted: Fri Nov 18, 2022 9:19 am
by Tony Li
Hi,

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:

Code: Select all

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);
}

Re: Substring replacement

Posted: Fri Nov 18, 2022 2:58 pm
by korsak42
Yes, most likely it's in the order of function calls. I use a rather strange way to pass text:

1) The text from the string is passed to the TempString variable.
2) in the right node I use [var=TempString]

I can't use OnConversationLine because this type of text insertion is needed on certain nodes in certain dialogs, not all nodes.

Maybe I don't understand how OnConversationLine works.

Re: Substring replacement

Posted: Fri Nov 18, 2022 3:19 pm
by Tony Li
How do you know which is the right node?

For example, if the node has a special custom field value, then you could check that field in OnConversationLine:

Code: Select all

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);
    }
}

Re: Substring replacement

Posted: Fri Nov 18, 2022 3:36 pm
by korsak42
Is it possible to make custom variables in nodes??? This will make my job a lot easier! Could you elaborate on how this can be done?

Right now I just do this: Image

"Right node" is one after "Talk with strangers"

Re: Substring replacement

Posted: Fri Nov 18, 2022 3:57 pm
by korsak42
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?