Math on a screen [Solved]

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
DoubleProlix
Regular
Posts: 33
Joined: Thu Oct 19, 2017 8:04 pm
Contact:

Math on a screen [Solved]

#1 Post by DoubleProlix »

I'm setting up a HUD, and I want it to display the ratio of two variables:

text "Ratio: [x]"

works fine to display the value of var1. However, if I try to display the ratio of x to y:

text "Ratio: [x / (x + y)]"

it treats the equation like a name and throws an exception.

I don't need the screen to change the game state, just to display the statement's output. Can I do this, or do I need to create another variable for the ratio that is updated whenever x or y is changed? Or is there some simpler way to make this work?
Last edited by DoubleProlix on Sat Jun 23, 2018 7:39 pm, edited 1 time in total.
Current Project: Thaumarotica - an occult erotic thriller
SubscribeStar - Patreon - Itch.io

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Math on a screen

#2 Post by Alex »

The square bracets in text is used to show value of a variable not to calculate it. Try

Code: Select all

screen scr_name():
    $ res = x / (x + y)
    text "Ratio: [res]" 

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Math on a screen

#3 Post by Remix »

Code: Select all

screen a:
    $ x,y = 7.0, 12.0
    text "Ratio: {:.03f}".format( x / (x + y) )
Alternatively, pyformat can be used ( the :.03f means zero pad to 3 decimals )
Remember to cast x and y to floats rather than integers to avoid python pre 3 rounding the result
Last edited by Remix on Sat Jun 23, 2018 7:39 pm, edited 1 time in total.
Frameworks & Scriptlets:

DoubleProlix
Regular
Posts: 33
Joined: Thu Oct 19, 2017 8:04 pm
Contact:

Re: Math on a screen

#4 Post by DoubleProlix »

Right, that works, thanks.
Current Project: Thaumarotica - an occult erotic thriller
SubscribeStar - Patreon - Itch.io

Post Reply

Who is online

Users browsing this forum: No registered users