Conditional string

From Mount&Blade Modding Wiki
Jump to: navigation, search

A conditional string is an embedded expression in a string that evaluates to one of two possibilities based on the value contained in a given register. If the register contains a non-zero value, the first choice is used; otherwise the second choice is used. Either choice can be blank. Persons familiar with programming will recognise a conditional string as the ternary operator "?:".

Using conditional strings

Conditional strings are formatted by using the name of a register, followed by a question mark "?" and the string to use if true, followed by a colon ":" and the string to use as false, all surrounded by braces "{" and "}".

A common example of a conditional string is below:

town{reg0?s:}

If reg0 is not equal to 0, the embedded expression will yield "s", otherwise it will yield "" (a blank string). Thus, the final resulting string will be

towns

if reg0 is not equal to 0, or

town

if reg0 is equal to 0. This is the principal method of pluralisation in Mount and Blade dialogue and scripting.

Complex conditional strings

Conditional strings can be embedded within each other in any combination desired. For instance,

{reg6?I:{reg7?You:{s11}}}

will display "I" if register 6 is true, "You" if register 7 is true, or the contents of string register 11 (in this case containing the name of a lord) if neither are true. This is a compact and elegant method that is more space efficient than using a string register directly and then assigning "I", "You", or the name of a person to that register as needed.

As a matter of style, if a conditional string is embedded more than twice, it is generally better to separate that logic out and assign a string register using a try_begin/else_try/try_end block.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox