Ren'Py [Renpy] Can i make text result variable ?

rnot2000

Active Member
Game Developer
Mar 2, 2020
502
957
278
Hi guys i have a question...

Code:
screen test:
    text str("1 + 2")
How to make the text show "3" instead "1 + 2" ???
i want the text result variable
any idea ?
 

Winterfire

Conversation Conqueror
Respected User
Game Developer
Sep 27, 2018
6,384
9,155
800
remove the "

text str(1+2)

should be enough
 

Winterfire

Conversation Conqueror
Respected User
Game Developer
Sep 27, 2018
6,384
9,155
800
can i get the text result variable ?
What do you mean?

text str(1+2) should display 3, no?

If you want to store it somewhere, you can do:
$rnot = str(1+2)

then text rnot
 

HELIO CESAR

Well-Known Member
May 30, 2018
1,451
3,190
378
Yeah, kinda confusing what exactly you are wanting.
Winterfire instructions should help if you wanted to do a calculation inside renpy and show its value, but at this point its a hard coded calculation so simply making It print the result directly would be less work.

I am gonna assume that you are wanting that the player inform a expression to the renpy engine to calculate and after it stores the result.
If so you could use the eval function, like so;

UserInput = "1+4"
eval(UserInput)
Eval should in this case return the integer 5

You could make a nested call like:
UserInput = "1+4"
str(eval(UserInput))
Str returns 5 in a string

There are implications with eval safety but i believe for an offline renpy game it would only impact if the dev is concerned with cheating, maybe a more versed coder could comment on this.
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
12,726
20,889
1,026
Code:
screen test:
    text str("1 + 2")
Since quoted content is always a string, in you example you're asking to translate the literal string "1 + 2" into a string... As Winterfire said, the right syntax should be str( 1 + 2 ).

But I guess that this don't really help since what you want is probably not as basic.
Therefore, more globally:

If you want to display some computation as text from a screen, you can either use:

text str( thisVar + thatVar ) if both "thisVar" and "thatVar" are compatible type ( int/float + int/float, or string + string ).

text "{} {}".format( thisVar, thatVar ) if "thisVar" and "thatVar" are not compatible types, or if their type vary and can be not compatible.

Python:
screen whatever():  # note the highly recommended '()'

    $ myVar = "{} {}".format( sum( [ nb for ( kind, nb ) in inventory if kind == "apple" ] ), "apple" )
    text "you have [myVar]"
if you need to use more complex computation.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
12,726
20,889
1,026
There are implications with eval safety but i believe for an offline renpy game it would only impact if the dev is concerned with cheating, maybe a more versed coder could comment on this.
The issue exist even with offline game if you use a save coming from an unknown source.

Lets say that you have:
Python:
define numbersOf="sum( [ nb for ( kind, nb ) in inventory if kind == "{}" ] )"

eval( numbersOf.format( "apple" ) )
Someone malicious can perfectly use the console to change the value of "numbersOf" to
something like "makeComputerExplode()\n'{}'\n".
Like the value have changed, the variable is now savable. And like the value is saved, it will be updated for anyone loading this save.
Now, the first time the game will use eval( numbersOf.format( "apple" ) ), the computer will explode.


This being said, eval stay relatively limited in range of threat. All the code have to be defined in it, and I'm not sure that it would really be able to deal with something like:
eval( "def myFunc( param1, param2 ):\n [malicious code part1]\n return boom;dangerousThing( myFunc( {}, {} ) )\n" )

And, obviously, since Pickles itself is a possible threat, Ren'Py saves are now a bit more secured. If the player decided to bypass the restriction regarding third party saves, it's is responsibility.
 

HELIO CESAR

Well-Known Member
May 30, 2018
1,451
3,190
378
The issue exist even with offline game if you use a save coming from an unknown source.

Lets say that you have:
Python:
define numbersOf="sum( [ nb for ( kind, nb ) in inventory if kind == "{}" ] )"

eval( numbersOf.format( "apple" ) )
Someone malicious can perfectly use the console to change the value of "numbersOf" to
something like "makeComputerExplode()\n'{}'\n".
Like the value have changed, the variable is now savable. And like the value is saved, it will be updated for anyone loading this save.
Now, the first time the game will use eval( numbersOf.format( "apple" ) ), the computer will explode.


This being said, eval stay relatively limited in range of threat. All the code have to be defined in it, and I'm not sure that it would really be able to deal with something like:
eval( "def myFunc( param1, param2 ):\n [malicious code part1]\n return boom;dangerousThing( myFunc( {}, {} ) )\n" )

And, obviously, since Pickles itself is a possible threat, Ren'Py saves are now a bit more secured. If the player decided to bypass the restriction regarding third party saves, it's is responsibility.
Wow, not so versed coder even less in Renpy internal works, didn't though about the possibility trought saves, but as you said, at that point it would be more of an user mistake.
If my guess was truly what rnot2000 intentioned to do, maybe projects like (seems like a well thought pre-config of ast's eval) and a super restrictive filter to the string can help minimize risks to user and prevent cheating but as you reminded me, people are ingenious.