TextObject
General Info
The TextObject class is required to make in-game texts localizable. It would be a nice approach to use this class for any information that may ever be shown to a player. TextObject requires a string as a main value argument. It may be created using int or float arguments, in which case they are converted to string in the constructor.
String IDs
To become fully localizable, strings used in TextObjects should start with stringID:
String ID is a string key of arbitrary length and content with no spaces. It is inadvisable to use long stringIDs, as it said to affect performance. Also, string IDs are global, so even if two different strings with same string ID are created and used in different game modules, they would still conflict (and some pretty bad things may happen then). Therefore, it is inadvisable to regulary use semantically meaningful keys as string IDs. Instead, use X-digit alphanumeric string codes for that, which look as follows: {=zmR6sT03}
. As of now, TaleWords and most of the game modders use 8-digit alphanumeric string codes to generate string IDs in most cases. You can use this service to generate random keys: https://www.random.org/strings/ . Of course random matches are still possible, but quite unlikely.
More information about using string IDs in the game’s XML files can be found in Localization.
Text variables
Strings used in TextObjects could contain variables, which themselves are TextObjects. Variables are stored in the public property named Attributes:
Defining text variables
There are two ways to define a text variable for TextObject. You can do it in constructor, or with SetTextVariable
method:
Complex text variables and cases
As all text variables are TextObjects themselves, they could be quite complex. Note that any variables set to the nested TextObject could be used as properties in external TextObject. It is also worth noting that TextObjects do have basic built-in inline conditional processor. Consider following example:
Then using of GetMainTextObject(40, true).ToString();
would return following string:
Note on nested properties
Using of nested properties for complex objects could be very helpful to anyone translating your mod. Consider following example from TaleWorlds code:
Method StringHelpers.SetCharacterProperties
here is used to set several character-related attributes, that are used in the TextObject string. In this example character's Name and Gender are used. Adding such info to any string mentioning a character would be wise desision, as even if English phrasing you use does not require, for example, gender info, translation of the sentence to other language may have.
Be wary though, that using multiple levels of nesting for properties is not supported, so using of something like {NPC.CLAN.NAME}
is impossible - game text processor would return an empty sting, if you use several dots with one variable tag.
Global text variables
Sometimes you would need to use same info in several TextObjects. In that case you can define global text variable, that will be used by text processor for any TextObject when .ToString()
method is called. To do so you will have to use
MBTextManager is a public static class. Any variable set to it will be used by game text processor for any TextObject as if it was defined for that TextObject itself. If you intend to use global text variables in the TextObject, you should not set any local text variables to it. Game text processor only uses variables stored in the MBTextManager if TextObject instance does not have it's own attributes.
Default language
Default language for the game is English, so any localizable string you use in design time should be defined in English. That way it would never be read from any game files, but instead would be built in your assembly. See more information on mantaining your XML files with game strings in Localization.
Last updated