Page 1 of 1

Custom Text Conversion

Posted: Wed Mar 27, 2024 2:43 am
by WeiYiHua
Hello, I've implemented some custom text conversions in my project, such as converting "<TERM>Test/Player/Hello<TERM>!" to "Hello!" or "My name is {0}.<INSERT>A<INSERT>" to "My name is A." These conversions are performed using classes similar to PixelCrushers.UITextField, where the get and set of UITextField.text represent the text before conversion, and UnityEngine.UI.Text.text is immediately converted and assigned when UITextField.text is set.

Now, I'm trying to modify UITextField to expose an interface for supporting text conversion similar to the above. Would this approach conflict with DialogueSystem UI, for example, with AbstractTypewriterEffect? Thanks in advance!

Re: Custom Text Conversion

Posted: Wed Mar 27, 2024 12:24 pm
by Tony Li
Hi,

I strongly recommend against directly modifying the Dialogue System's code -- or any code that's not your own project's code -- because you'll lose your modifications when you update the asset.

Are these custom tags in your dialogue text? If so, then the most common approach is to add a script with an OnConversationLine(Subtitle) method to your Dialogue Manager. For example, to replace the tag "{weather}" in the dialogue text "The newspaper says the weather today is going to be {weather}.":

Code: Select all

void OnConversationLine(Subtitle subtitle)
{
    subtitle.formattedText.text = subtitle.formattedText.text.Replace("{weather}", GetWeatherReport());
}
If this isn't what you need to do, let me know what areas of text you need to change, and I'll suggest a way to do it without directly modifying the Dialogue System's code. For example, if you don't want to use OnConversationLine(Subtitle) for some reason, you could make a subclass of StandardUISubtitlePanel and override the SetContent() method:

Code: Select all

public class MySubtitlePanel : StandardUISubtitlePanel
{
    public override void SetContent(Subtitle subtitle)
    {
        // Your code to change subtitle.formattedText.text here.
        base.SetContent(subtitle);
    }
}
(To replace the component in-place and retain UI element assignments, see here.)

Re: Custom Text Conversion

Posted: Wed Mar 27, 2024 7:40 pm
by WeiYiHua
Thank you for giving me a different perspective. Since this kind of conversion needs to be applied widely across the database and typically only happens once at the start of the game, if I want to avoid disrupting the DialogueSystem UI and avoid subclassing many UI components, can I directly filter and convert the fields of the database during game initialization?

I've looked into the code of the DialogueDatabase, and in this case, if I do so, the DatabaseManager has events like OnBeforeDatabaseAdd, OnAfterDatabaseAdd, OnBeforeDatabaseRemove, and OnAfterDatabaseRemove, which should allow for more efficient handling of this matter (similar to a processing pipeline for the database).

Re: Custom Text Conversion

Posted: Wed Mar 27, 2024 8:42 pm
by Tony Li
Hi,

If you're only using one dialogue database, you can wait until the Dialogue Manager has initialized (wait for DialogueManager.instance.initializationComplete) then change the text, and then call:

Code: Select all

DialogueLua.InitializeChatMapperVariables();
DialogueLua.AddChatMapperVariables(DialogueManager.masterDatabase, new List<Database>());

Re: Custom Text Conversion

Posted: Wed Mar 27, 2024 9:17 pm
by WeiYiHua
Thank you, very helpful.

Re: Custom Text Conversion

Posted: Wed Mar 27, 2024 9:28 pm
by Tony Li
Glad to help!