Remembering random coin drops

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
city
Newbie
Posts: 23
Joined: Mon Jul 04, 2016 6:30 pm
Contact:

Remembering random coin drops

#1 Post by city » Fri Jul 08, 2016 2:52 pm

Is there a way to record randomly dropped money?

Code I'm currently using:

Code: Select all

init python:
    showmoney= True
    money = 0
    def display_money():
        if showmoney:   
            ui.frame() #This is optional. It adds a frame around the text.
            ui.text("$ %d" %money)
    config.overlay_functions.append(display_money)
and then:

Code: Select all

    $ randcoin = renpy.random.choice(['27', '32', '38', '44', '19', '21', '35', '17', '29', '51', '25', '31', '7', '33', '22', '34', '28', '13'])

        "you received %(randcoin)s coins."    
What code do I need in order to sync these two up so that the coins that get dropped get added to the total money counter?

User avatar
morg
Regular
Posts: 95
Joined: Sun May 10, 2015 7:35 am
Projects: Cupid's Affair
Tumblr: n-morg
Contact:

Re: Remembering random coin drops

#2 Post by morg » Fri Jul 08, 2016 5:03 pm

I don't exactly get what you mean by drop money (like in the street?) if you mean that you want the player to earn a random amount of money you could use this;

Code: Select all

  
screen count:
    text "Money: [money]"
label then:
    $ money = 10
    show screen count
    $ randcoin = renpy.random.randint(15, 40) #picks a random number from 15 to 40
    "You currently have [money] coins"
    "You've found [randcoin] coins!"
    $ money += randcoin 
    "Now you have a total of [money] coins"
renpy.random.randit is for picking numbers, whilst renpy.random.choice is for statements.
Is this what you wanted?
Image

city
Newbie
Posts: 23
Joined: Mon Jul 04, 2016 6:30 pm
Contact:

Re: Remembering random coin drops

#3 Post by city » Fri Jul 08, 2016 5:46 pm

Awesome. That's exactly what I needed. I edited it a little bit:

Code: Select all

init python:
    showmoney= True
    money = 0
    def display_money():
        if showmoney:   
            ui.frame() #This is optional. It adds a frame around the text.
            ui.text("$ %d" %money)
    config.overlay_functions.append(display_money)

label start:

$ money = 0

$ randcoin = renpy.random.randint(15, 40) #picks a random integer between
        
"you've found [randcoin] coins!"
           
$ money += randcoin

This first part makes a little framed box that shows how much money is currently owned, and your code allows me to add and subtract from it with the randomized integers, which is exactly what I needed. Thank you!

city
Newbie
Posts: 23
Joined: Mon Jul 04, 2016 6:30 pm
Contact:

Re: Remembering random coin drops

#4 Post by city » Tue Jul 12, 2016 1:27 pm

I'm having a bit of a problem with this...

The first [randcoin] is always random, but every time I use [randcoin] afterwards, it always uses the first number rolled.

For example:

Code: Select all


label start:

     "You received [randcoin] coins!" #Let's say it's 15

     "You received [randcoin] coins!" #It'll give me 15 again every time

If I refresh, it'll give me a different starting number, but then repeat *that* number every time.

Any help?

User avatar
theCodeCat
Regular
Posts: 62
Joined: Sun Sep 06, 2015 8:40 pm
Projects: Lucid9, Mystic Destinies: Serendipity of Aeons
Skype: theCodeCat
Contact:

Re: Remembering random coin drops

#5 Post by theCodeCat » Tue Jul 12, 2016 5:49 pm

"renpy.random.randint(15, 40)" is the part that generates the random number, "randcoin" is just the variable used to hold the value.

Whenever you need a new random value you'll need to call "renpy.random.randint(15, 40)" again.

Ex:

Code: Select all

label start:
    $ money = 0

    $ randcoin = renpy.random.randint(15, 40)
    $ money += randcoin
    "you've found [randcoin] coins!"

    $ randcoin = renpy.random.randint(15, 40)
    $ money += randcoin
    "you've found another [randcoin] coins!"

city
Newbie
Posts: 23
Joined: Mon Jul 04, 2016 6:30 pm
Contact:

Re: Remembering random coin drops

#6 Post by city » Tue Jul 12, 2016 5:53 pm

Ohhhhh thanks!

Post Reply

Who is online

Users browsing this forum: Google [Bot]