Because it seems like it may be of interest, here's a brief explanation of why this happens (and even why it's not considered a bug). Hope you find it helpful and/or interesting. :)
If you're using the default history screen found in the
screens.rpy file created when starting a project, then substitution is disabled when displaying historical log entries by default. This is by design. When Ren'Py writes the log itself, it saves the line after doing substitutions, at the time it is shown to the player. This is to ensure that if a variable changes later on, the historical log is still an accurate representation of that was on screen for the player at the time. i.e. The history screen disables substitution because the expectation is that any text in the log has already been processed and shouldn't be processed a second time.
In light of this, it starts to make sense why your original attempt to introduce a templated log entry via code didn't work as expected. The solution presented by Imperf3kt works because it mirrors Ren'Py's behaviour of inserting a log entry where the replacement has already been done (Python's
% operator takes care of this when the line is executed).
Finally, an alternative solution to this could be to use
renpy.substitute, which would call Ren'Py's substitution code which is typically applied behind the scenes when a message is displayed to handle your replacements. As a happy side bonus, it would also apply translations if appropriate. While there's nothing wrong with the
% operator solution, in cases where you're dealing with multiple values that must be replaced, using the built-in system might be easier to maintain over time as you choose to add or remove templated variables from the message inserted into history.
Below is an example using substitute, and a version with the literal message wrapped in the
_() marker function so that it will show up in the translation system:
Code: Select all
# Just substitution on it's own.
$ narrator.add_history(kind='nvl', who=narrator.name, what=renpy.substitute('hello [variablename1]'))
# Version where 'hello [variablename1]' will be captured for translation and auto-translated if a translation is provided.
$ narrator.add_history(kind='nvl', who=narrator.name, what=renpy.substitute(_('hello [variablename1]')))