[solved] An issues with narrator.add_history - doesn't register values of variables

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Aureus
Veteran
Posts: 271
Joined: Sun Aug 06, 2017 4:49 am
Completed: The Tavern, Tales From Windy Meadow
Projects: Roadwarden
Organization: Moral Anxiety Studio
itch: moral-anxiety
Location: Breslau, Poland
Contact:

[solved] An issues with narrator.add_history - doesn't register values of variables

#1 Post by Aureus » Tue Jan 28, 2020 9:52 am

Greetings! I'm greatly puzzled by this detail.

If I run a command like this one:

Code: Select all

$ narrator.add_history(kind='nvl', who=narrator.name, what='test test test')
It works fine. It displays "test test test" in the history screen, which is fairly similar to the "basic" Ren'Py template. It even keeps tags like {color}, just like the regular text. All cool.

However, if I do something like like this:

Code: Select all

$ narrator.add_history(kind='nvl', who=narrator.name, what='[variablename1]')
The history screen displays it as "[variablename1]". For some reason. Even though if the [variablename1] appears in the regular in-game text, even the history screen displays the value of this variable instead. a number, a word, a phrase...

Is there something I can do to make it work?
Last edited by Aureus on Tue Jan 28, 2020 4:32 pm, edited 1 time in total.
Tales From Windy Meadow - slice of life, pixel art Visual Novel set in a fantasy village. now available on Steam.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3636
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: An issues with narrator.add_history - doesn't register values of variables

#2 Post by Imperf3kt » Tue Jan 28, 2020 3:19 pm

It's because you're using a python function. Try this instead.

Code: Select all

$ narrator.add_history(kind='nvl', who=narrator.name, what='%s' %variablename1)


It works in a game I use it in.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor
Free Android GUI - Updated occasionally
Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py

User avatar
Aureus
Veteran
Posts: 271
Joined: Sun Aug 06, 2017 4:49 am
Completed: The Tavern, Tales From Windy Meadow
Projects: Roadwarden
Organization: Moral Anxiety Studio
itch: moral-anxiety
Location: Breslau, Poland
Contact:

Re: An issues with narrator.add_history - doesn't register values of variables

#3 Post by Aureus » Tue Jan 28, 2020 3:55 pm

Imperf3kt wrote:
Tue Jan 28, 2020 3:19 pm

Code: Select all

$ narrator.add_history(kind='nvl', who=narrator.name, what='%s' %variablename1)
Thank you! It partially works. Or it's possible that it completely works, I just don't know yet how should I phrase it properly.

Could you give me an example how could I use this command to create a text like "test test test [variablename1] test test test"?

As far as my nonexistent abilities lead me, copy-pasting your code allows me to display the variable alone, but even though I tried various things, I can't add anything to it.
Tales From Windy Meadow - slice of life, pixel art Visual Novel set in a fantasy village. now available on Steam.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3636
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: An issues with narrator.add_history - doesn't register values of variables

#4 Post by Imperf3kt » Tue Jan 28, 2020 4:26 pm

If you want the variable mid sentence, just use the %s operand as if it were the word, don't wrap it on square brackets like you would in a string.

"Test my %s is working" %variablename1
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor
Free Android GUI - Updated occasionally
Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py

User avatar
Aureus
Veteran
Posts: 271
Joined: Sun Aug 06, 2017 4:49 am
Completed: The Tavern, Tales From Windy Meadow
Projects: Roadwarden
Organization: Moral Anxiety Studio
itch: moral-anxiety
Location: Breslau, Poland
Contact:

Re: An issues with narrator.add_history - doesn't register values of variables

#5 Post by Aureus » Tue Jan 28, 2020 4:32 pm

Thank you so much, Imperf3kt, this is just... perfect. I was thinking about this bug for like a month and you're saving me. <3
Tales From Windy Meadow - slice of life, pixel art Visual Novel set in a fantasy village. now available on Steam.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3636
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: [solved] An issues with narrator.add_history - doesn't register values of variables

#6 Post by Imperf3kt » Tue Jan 28, 2020 4:49 pm

Thanks, but the real credit goes to the other members of this forum.
I only learnt of this behaviour myself a couple of months ago.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor
Free Android GUI - Updated occasionally
Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py

strayerror
Regular
Posts: 154
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: [solved] An issues with narrator.add_history - doesn't register values of variables

#7 Post by strayerror » Tue Jan 28, 2020 9:56 pm

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]')))

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3636
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: [solved] An issues with narrator.add_history - doesn't register values of variables

#8 Post by Imperf3kt » Tue Jan 28, 2020 10:46 pm

nice, thanks for the explanation and pointing out an improved way of doing it.
definitely appreciated.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor
Free Android GUI - Updated occasionally
Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py

User avatar
Aureus
Veteran
Posts: 271
Joined: Sun Aug 06, 2017 4:49 am
Completed: The Tavern, Tales From Windy Meadow
Projects: Roadwarden
Organization: Moral Anxiety Studio
itch: moral-anxiety
Location: Breslau, Poland
Contact:

Re: [solved] An issues with narrator.add_history - doesn't register values of variables

#9 Post by Aureus » Wed Jan 29, 2020 2:25 am

Thank you, strayerror, you are able to explain things even to a casual like me. I've already spent an hour applying the previous solution, but I'll definitely give your code a shot later on.
Tales From Windy Meadow - slice of life, pixel art Visual Novel set in a fantasy village. now available on Steam.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]