Text interpolation
Ren'Py can replace parts of certain text displayables with the values
of variables
by including the name of the variable with square brackets, much
like the way Python's string.format
and
f-strings work with
curly brackets.
As curly brackets were already being used for
text tags, square brackets are used
for interpolation.
barman "What can I get you [pcName]? Your usual?"
Barman
What can I get you Fred? Your usual?
Note: This interpolation takes place when the text is displayed, so it works for menus, say statements, and text displayables for example, but it is not universal; it can't be used in lines of Python.
Conversion characters
Optional conversion characters can be used to change the case of the substituted text, require it to be translated, or be checked for interpolations. This next example converts to player's name to all caps:
barman "GET OUT [pcName!u]! YOU'RE BARRED."
Barman
GET OUT FRED! YOU'RE BARRED.
Format specification
A format specifier can be used to
control the formatting of a field. In this example forcing the
integer values to be padded with 0
to two digits.
$ hour = 2 $ min = 5 "It is now [hour:0=2]:[min:0=2]."
It is now 02:05.
Field specification
The full detail of the field specification between the square
brackets consists of a number of parts in the following order:
Note: This order is different to that used by the Python equivalents
with the conversion characters following the format specification.
The conversion is still done before the formatting though.
-
Field selector
The name of a variable in the store. This can be followed by zero or more.attribute_name
or[element_index]
to select attributes or indexes of the supplied field.
Note: You cannot invoke normal functions or methods inside a field selector, and you can't write expressions either. Sometimes you will have to use temporary variables to hold computed values. -
Format specification
Optionally a:
followed by a format specifier. This allows detailed control over how the field is formatted. -
Conversion
Optionally a!
followed by one or more conversion characters:Character Meaning a
Calls ascii()
on the selected field before formatting. This is similar torepr()
except any non-ascii characters are replaced with escape codes.c
Capitalise the first letter. i
Recursive interpolation causes any interpolation patterns in the field to also be evaluated. l
Convert to lowercase. q
Causes text tags to be quoted before formatting. r
Calls repr()
on the selected field before formatting. In many cases this will return a string that could be used in python source to define the value. For example strings have single quotes at the start and end.s
Calls str()
on the selected field before formatting.t
Translates the selected field before formatting. u
Convert to uppercase. -
r
ors
forrepr()
orstr()
-
t
to translate -
i
for recursive interpolation -
q
for quoting -
u
to convert to uppercase -
l
to convert to lowercase -
c
to capitalise the first letter
Irrespective of the order of the conversion characters they are applied in the following order:
Screens
An important distinction between Ren'Py's text interpolation and that offered by the Python operations is that it is dynamic. When text interpolation is used in a screen the resulting rendered text can change when the underlying variable changes.