Addition and substraction

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
Magnus
Regular
Posts: 57
Joined: Sat Sep 24, 2011 5:39 pm
Contact:

Addition and substraction

#1 Post by Magnus » Sun Oct 28, 2012 6:46 am

Hello everybody

A quick question: how do you add and subtract in a Ren'Py game ?

Look at the simple code below:

Code: Select all

label start:

    $ coinseric = 0
    $ coinsdaisy = 0 
    
label shop_one:
    $ coinseric = 10
    "Eric wants to buy or sell something. Eric has %(coinseric)d coins."
    
    menu:
        "buy apple for 5 dollars":
            $ coinseric -= 5            
            "Eric bought an apple."
            "Eric has %(coinseric)d coins."
        "buy banana for 2 dollars.":
            $ coinseric -= 2            
            "Eric bought a banana."
            "Eric has %(coinseric)d coins."
        "Sell pie for 10 dollars.":
            $ coinseric += 10            
            "Eric sold a pie."
            "Eric has %(coinseric)d coins."
            
label shop_two:
    $ coinsdaisy = 5
    "His sister Daisy also wants to buy or sell something. Daisy has %(coinsdaisy)d coins."
    menu:
        "buy apple for 5 dollars":
            $ coinsdaisy -= 5            
            "Daisy bought an apple."
            "Daisy has %(coinsdaisy)d coins."
        "buy banana for 2 dollars.":
            $ coinsdaisy -= 2            
            "Daisy bought a banana."
            "Daisy has %(coinsdaisy)d coins."
        "Sell pie for 10 dollars.":
            $ coinsdaisy += 10            
            "Daisy sold a pie."
            "Daisy has %(coinsdaisy)d coins."

label sum:
    "Together they have %(coinseric)d and %(coinsdaisy)d coins."
            
    return
How can Ren'Py automatically give me the total of the coins ? I know it's not difficult, but I can't seem to find it...

Thanks in advance.

User avatar
TrickWithAKnife
Eileen-Class Veteran
Posts: 1261
Joined: Fri Mar 16, 2012 11:38 am
Projects: Rika
Organization: Solo (for now)
IRC Nick: Trick
Location: Tokyo, Japan
Contact:

Re: Addition and substraction

#2 Post by TrickWithAKnife » Sun Oct 28, 2012 6:55 am

This should be in the Ren'Py Q&A section.

Code: Select all

    $ totalcoins = coinseric + coinsdaisy
"We must teach them through the tools with which they are comfortable."
The #renpy IRC channel is a great place to chat with other devs. Due to the nature of IRC and timezone differences, people probably won't reply right away.

If you'd like to view or use any code from my VN PM me. All code is freely available without restriction, but also without warranty or (much) support.

Magnus
Regular
Posts: 57
Joined: Sat Sep 24, 2011 5:39 pm
Contact:

Re: Addition and substraction

#3 Post by Magnus » Sun Oct 28, 2012 9:18 am

Sorry and thanks, works like a charm !

One more little question, and I'll be off.

Let's say I have a total of 20 coins. Depending on the amount of coins I have, the story will go in a certain direction.

If the total of coins is more than 20, the story follows path a, if it is less than 20, it follows path b and so on.

Any idea how to do that ?

Like always, thanks in advance.

User avatar
KomiTsuku
Eileen-Class Veteran
Posts: 1023
Joined: Mon Sep 22, 2008 11:32 pm
Completed: Dreams of the Skies, Anton's Vacation, Luka, The Halberd and The Tiger, Rising Angels, Pyrite Heart, Rising Angels: Reborn, The Halberd and The Fox, VN Tycoon, RA: Hope
Projects: Rising Angels
Organization: IDHAS Studios
IRC Nick: Komi
itch: idhas
Location: Somewhere
Contact:

Re: Addition and substraction

#4 Post by KomiTsuku » Sun Oct 28, 2012 9:24 am

Code: Select all

if(totalcoins>20):
     jump labelA
if(totalcoins==20):
     jump endOfTheWorld
else:
     jump labelB
Does renpy support elseif?

Magnus
Regular
Posts: 57
Joined: Sat Sep 24, 2011 5:39 pm
Contact:

Re: Addition and substraction

#5 Post by Magnus » Sun Oct 28, 2012 9:31 am

Works !

(bows)

Thanks everybody.

User avatar
Hiddlestoners
Regular
Posts: 126
Joined: Sun Jul 01, 2012 9:27 am
Completed: Thinking Of You [KN]
Projects: PERPLEXITY [Mystery VN], simplicity [KN,Fluff,GxB,BxG]
Contact:

Re: Addition and substraction

#6 Post by Hiddlestoners » Sun Oct 28, 2012 9:37 am

KomiTsuku wrote:Does renpy support elseif?
Wouldn't it just be elif?
ENOSHIMA BOWL!
Image

User avatar
KomiTsuku
Eileen-Class Veteran
Posts: 1023
Joined: Mon Sep 22, 2008 11:32 pm
Completed: Dreams of the Skies, Anton's Vacation, Luka, The Halberd and The Tiger, Rising Angels, Pyrite Heart, Rising Angels: Reborn, The Halberd and The Fox, VN Tycoon, RA: Hope
Projects: Rising Angels
Organization: IDHAS Studios
IRC Nick: Komi
itch: idhas
Location: Somewhere
Contact:

Re: Addition and substraction

#7 Post by KomiTsuku » Sun Oct 28, 2012 9:46 am

Hiddlestoners wrote:
KomiTsuku wrote:Does renpy support elseif?
Wouldn't it just be elif?
I guess so. Curse you, python (ruby as well), for being different than everyone else!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Ocelot