Replace subtitle content on the fly

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
DragoonHP
Posts: 62
Joined: Tue Jan 15, 2019 8:17 am

Replace subtitle content on the fly

Post by DragoonHP »

Is there a way to swap words and phrases on the fly? In game, some characters can only be understood if the player has learnt their language so I was wondering if there is a quick and easy way of replacing text?

Is using

Code: Select all

OnConversationLine(Subtitle subtitle)
and then changing subtitle.formattedText the way to go?
Thanks.
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: Replace subtitle content on the fly

Post by Tony Li »

Yes, that's the best way if you're replacing the entire text.

If you're only replacing words or phrases, another way is to write a C# function and register it with Lua, such as:

Code: Select all

static string Translate(string word) { // (example)
    if (playerKnowsLanguage) return word;
    else if (word == "rock") return "blaboo"
    else if (word == "water") return "flizzit";
    else return "???";
}

void Awake() {
    Lua.RegisterFunction("Translate", this, SymbolExtensions.GetMethodInfo(() => Translate(string.Empty)));
}
Then in your Dialogue Text, use [lua(Translate("word"))], such as:
  • Dialogue Text: "Would you like some [lua(Translate("water"))]?"
DragoonHP
Posts: 62
Joined: Tue Jan 15, 2019 8:17 am

Re: Replace subtitle content on the fly

Post by DragoonHP »

Okay.

I think I will use the subtitle method as its very flexible compared to the lua translate.
Thanks for the quick reply
Post Reply