Hard to believe but why can't I do maths?

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
Shintaro
Newbie
Posts: 5
Joined: Thu Aug 16, 2007 8:08 pm
Location: Adelaide, South Australia
Contact:

Hard to believe but why can't I do maths?

#1 Post by Shintaro » Thu Aug 16, 2007 11:17 pm

Hi Everyone,

New here and been reading up on the engine, even made a story etc so far very impressed with engine and want to do more with it.

But I want to do a simple bit of maths ... and I haven't found out how to do the following (done lots of searching etc) and I can make characters bounce and put in new buttons and show movies but I can't do maths!!!!!

Basic idea is this ...

Code: Select all

 $ x = renpy.input("Please enter a NUMBER: ")       #This works
Next take the input and treat it as a numeric and multiply it by itself (without going into python - haven't made that work either)

Code: Select all

$ xx = x*x   #Doesn't work
or

Code: Select all

$ xx = %(x)d*%(x)d   #Doesn't work and various variants
What simple bit of arcane insight am I missing?

Cheers


PS How many PyTom's are there really? No on can do all the stuff he does and have a job!!!
Breathe in - Breathe out. Now once again ... breathe in - breathe out. Addictive isn't it?

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Hard to believe but why can't I do maths?

#2 Post by PyTom » Fri Aug 17, 2007 12:36 am

Basically, renpy.input reads in strings. Python is a strongly typed language, so you can't multiply two strings together. (What is "pantsu" * "ninja"?) You need to turn those strings into numbers for them to work.

In Python, there are two types of numbers, ints and floats. Ints are integers, whole numbers, numbers like -1, 0, 1, and 42. Floats are numbers that might have a fractional part**, like -1.0, 0.5, 42.69, 70.0 and so on.

To turn variable x into an int, run the int() function on it:

x = int(x)

To turn it into a float, use float:

x = float(x)

Note that (for most operations) int op int is an int, while anything involving a float is a float. For example:

3 / 2 == 1
3.0 / 2 == 1.5
3 / 2.0 == 1.5
3.0 / 2.0 == 1.5

*Complex numbers are the devil's work, I say! And we're still on 2.3, which doesn't have decimals.

** Although it may not be possible for the computer to represent those fractions precisely.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
Enerccio
Miko-Class Veteran
Posts: 616
Joined: Thu Oct 26, 2006 4:23 pm
Projects: My Teacher; Songs of Araiah; Something new; Possible Cross Bomber?
Location: Slovakia, Kosice
Contact:

Re: Hard to believe but why can't I do maths?

#3 Post by Enerccio » Fri Aug 24, 2007 12:53 am

PyTom wrote:What is "pantsu" * "ninja"?
2x2 Shinobuden? :P
Image
http://www.bishojo.tk is technically ONLINE!
Songs of Araiah promo: http://www.youtube.com/watch?v=CalchucuoDU

themocaw
Regular
Posts: 106
Joined: Mon Aug 06, 2007 10:58 am
Contact:

Re: Hard to believe but why can't I do maths?

#4 Post by themocaw » Sat Aug 25, 2007 2:35 am

All right, so here is a related question.

Let's say I've got two stats: A and B. And I want to use both A and B, but I also want a third stat, C, which is equal to A minus B. How would I set that up using the functions you describe?

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Hard to believe but why can't I do maths?

#5 Post by PyTom » Sat Aug 25, 2007 10:58 am

$ c = a - b

In every place where a or b is updated. (Trying to make this sort of thing automatic is actually the subject of my forum spam.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

Tayruh

Re: Hard to believe but why can't I do maths?

#6 Post by Tayruh » Sun Sep 02, 2007 1:48 am

I'm new to Renpy, but doesn't python let you use getters and setters? I would assume it'd be easy to make C automatically update if you put a setter on A and B. It's overly complicated for most people though, I know..

Tayruh

Re: Hard to believe but why can't I do maths?

#7 Post by Tayruh » Sun Sep 02, 2007 1:50 am

Oops. I'm silly. Putting a getter on C would be a much smarter idea.

themocaw
Regular
Posts: 106
Joined: Mon Aug 06, 2007 10:58 am
Contact:

Re: Hard to believe but why can't I do maths?

#8 Post by themocaw » Sun Sep 02, 2007 4:56 pm

PyTom wrote:$ c = a - b

In every place where a or b is updated. (Trying to make this sort of thing automatic is actually the subject of my forum spam.)
It might actually be easier to do it just before every place that c is checked for. . . anyway. Thanks for the heads up.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], _ticlock_