Custom Text Conversion

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
WeiYiHua
Posts: 7
Joined: Mon Mar 25, 2024 10:27 pm

Custom Text Conversion

Post 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!
User avatar
Tony Li
Posts: 20632
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom Text Conversion

Post 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.)
WeiYiHua
Posts: 7
Joined: Mon Mar 25, 2024 10:27 pm

Re: Custom Text Conversion

Post 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).
User avatar
Tony Li
Posts: 20632
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom Text Conversion

Post 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>());
WeiYiHua
Posts: 7
Joined: Mon Mar 25, 2024 10:27 pm

Re: Custom Text Conversion

Post by WeiYiHua »

Thank you, very helpful.
User avatar
Tony Li
Posts: 20632
Joined: Thu Jul 18, 2013 1:27 pm

Re: Custom Text Conversion

Post by Tony Li »

Glad to help!
Post Reply