Can Text Table fields include variables for dynamic text?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Andy Miira
Posts: 4
Joined: Wed Dec 23, 2020 11:11 pm

Can Text Table fields include variables for dynamic text?

Post by Andy Miira »

Hi! Let's say I have a text table field, "Game Version", I want it to show the following text in my UI:

Code: Select all

Game version: 1.0.0
But instead of the 1.0.0, I want it to fetch the game version dynamically, using Unity's "Application.version".

Is there any way to set custom dynamic variables inside Text Table fields?
Should I use Lua functions for that purpose?
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Can Text Table fields include variables for dynamic text?

Post by Tony Li »

Hi,

I'm not sure that you'd want to use text tables or Lua functions for that. But if you want to, you can register a Lua function to return the current version:

Code: Select all

Lua.RegisterFunction("GetAppVersion", null, SymbolExtensions.GetMethodInfo(() => GetAppVersion()));

static string GetAppVersion() { return Application.version; }
Then make a subclass of LocalizeUI that overrides GetLocalizedText() to parse [var=variable] and [lua(code)] tags:

Code: Select all

public class LocalizeUIWithTags : PixelCrushers.LocalizeUI
{
    protected override string GetLocalizedText(string fieldName)
    {
        return FormattedText.ParseCode(base.GetLocalizedText(fieldName));
    }
}
(Edit: Fixed typo.)

Then you can set a UI Text or TextMeshProUGUI field to:

Code: Select all

Game version: [lua(GetAppVersion())]
Andy Miira
Posts: 4
Joined: Wed Dec 23, 2020 11:11 pm

Re: Can Text Table fields include variables for dynamic text?

Post by Andy Miira »

I want to use Text Tables in this case, because I also needed to localize the "Game Version:" part of the string field.

Overall, I wanted the option to parse dynamic variables/Lua code inside Text Table fields (similarly to the Dialogue texts), and the code you posted works great!
Thanks :D

I just had to change the override access to "protected", and it all worked perfectly!

Code: Select all

protected override string GetLocalizedText(string fieldName)
User avatar
Tony Li
Posts: 22051
Joined: Thu Jul 18, 2013 1:27 pm

Re: Can Text Table fields include variables for dynamic text?

Post by Tony Li »

Oops; typo. I just fixed it in my post above.
Post Reply