persistant random number generator, how to?

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
VorrAkkagi
Newbie
Posts: 21
Joined: Tue Dec 11, 2018 5:41 am
Contact:

persistant random number generator, how to?

#1 Post by VorrAkkagi » Thu Dec 20, 2018 6:52 pm

Hi all,
I'm wondering what is the best way to create and call a random number generator and how to properly use it. I'm currently using this code:

$ d20roll = renpy.random.randint(1, 20) #this is what I want to call

menu: #this is what I've hacked together. Been looking all over for example of how to do this properly with no luck =/
"Peek in the front window":
$ d20roll
if d20roll > 15:
$ sneek +=1
jump frontdoor

"Walk in and see first hand":
jump frontdoor


And can someone point out to me to properly post code here please? I don't know the method...

DannX
Regular
Posts: 98
Joined: Mon Mar 12, 2018 11:15 am
Contact:

Re: persistant random number generator, how to?

#2 Post by DannX » Fri Dec 21, 2018 9:39 am

To post code here, make use of the [code] and [/code] tags, like this:
[code]
Paste your code here
[/code]

As for your question, I'm not sure I understand correctly, but each time you want to roll the dice, you call the function again.

Code: Select all

menu:
    "Peek in the front window":
        $ d20roll = renpy.random.randint(1,20) #this sets d20roll to a new value between 1 and 20. 
        if d20roll > 15:
            $ sneek +=1
            jump frontdoor

VorrAkkagi
Newbie
Posts: 21
Joined: Tue Dec 11, 2018 5:41 am
Contact:

Re: persistant random number generator, how to?

#3 Post by VorrAkkagi » Fri Dec 21, 2018 2:47 pm

Ah thanks for the code pasting info! =)

So quick question. I have defined the d20 roll at the top of my code with other defines using the '$' in this case. Are you saying this will keep the same value unless I reset it every time? If so, it would seem most efficient to keep the code up top behind a '#' so I can just copy/paste every time I need it. I was hoping to just be able to call it every time, rather than rewrite it. But, this is not so bad either =) Thank you very much!!

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

Re: persistant random number generator, how to?

#4 Post by Alex » Fri Dec 21, 2018 5:08 pm

You see $-sign means that there will be single-line python code.

Code: Select all

$ d20roll = renpy.random.randint(1,20)
is just the variable value asignment, it occures when this line executes. To re-assign value you need to run this line once again or make another similar line of code.

If you don't want to type this line again and again, you can create a function, like

Code: Select all

init python:
    def my_roll(d=6):
        return renpy.random.randint(1,d)

label start:
    "..."
    $ d6 = my_roll()
    $ d20 = my_roll(20)
    $ d100 = my_roll(100)
    
    "d6 - [d6], d20 - [d20], d100 - [d100]"
    "?"
    jump start

VorrAkkagi
Newbie
Posts: 21
Joined: Tue Dec 11, 2018 5:41 am
Contact:

Re: persistant random number generator, how to?

#5 Post by VorrAkkagi » Sat Dec 22, 2018 12:05 am

Oh thanks!! This will help save a lot of time =)

Post Reply

Who is online

Users browsing this forum: badanni, Google [Bot]