Problem with renpy.random

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
User avatar
minyan
Miko-Class Veteran
Posts: 630
Joined: Tue Feb 10, 2015 3:59 pm
Completed: Trial By Fire, Heartbaked, Ellaria, Plain, This My Soul, The Pretenders Guild
Projects: Arena Circus
Tumblr: minyanstudios
itch: harlevin
Contact:

Problem with renpy.random

#1 Post by minyan »

Hi, so I'm trying to use renpy.random to generate some random lines of dialogue, or for nothing to happen, whenever a certain button is pressed. The code I have works, but only once- the next time the button is pressed, it uses the same option as what was chosen before. How can I use renpy random to generate more than one outcome?

Code: Select all

    $ randomconvo = renpy.random.randint(1,6)

    if randomconvo == 3:
        "blugh
    elif randomconvo == 2:
        "bleh"
    elif randomconvo == 6:
        "blah
Thanks in advance for the help!
ImageImage

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

Re: Problem with renpy.random

#2 Post by Alex »

To make it work you need to re-run this line

Code: Select all

$ randomconvo = renpy.random.randint(1,6)
each time you need a random number.
So, you need to show more code: how did you make your certain button and where in the game flow you need to get this random dialogs.

User avatar
minyan
Miko-Class Veteran
Posts: 630
Joined: Tue Feb 10, 2015 3:59 pm
Completed: Trial By Fire, Heartbaked, Ellaria, Plain, This My Soul, The Pretenders Guild
Projects: Arena Circus
Tumblr: minyanstudios
itch: harlevin
Contact:

Re: Problem with renpy.random

#3 Post by minyan »

Alex wrote:To make it work you need to re-run this line

Code: Select all

$ randomconvo = renpy.random.randint(1,6)
each time you need a random number.
So, you need to show more code: how did you make your certain button and where in the game flow you need to get this random dialogs.
so the code is under a label that's called every time a button is pressed. The $randomconvo variable is assigned right under the label, so I assumed it would be rerun each time the jump to that label was made, but it hasn't worked that way.

so here's the button:

Code: Select all

                            textbutton "[name] action Jump("Commentary")
and the label

Code: Select all


label Commentary:

    $ randomconvo = renpy.random.randint(1,10)


ImageImage

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

Re: Problem with renpy.random

#4 Post by Alex »

Hm, it should work...
How's your code different from that sample?

Code: Select all

screen bttn_scr():
    textbutton "Press Me" action Jump("my_label") align (0.5,0.05)
    
label my_label:
    $ var = renpy.random.randint(1,3)
    if var == 1:
        "...[var]..."
    elif var == 2:
        "___[var]___"
    else:
        "~~~[var]~~~"
    jump start
    
label start:
    scene black
    show screen bttn_scr
    "..."
    "?"

User avatar
minyan
Miko-Class Veteran
Posts: 630
Joined: Tue Feb 10, 2015 3:59 pm
Completed: Trial By Fire, Heartbaked, Ellaria, Plain, This My Soul, The Pretenders Guild
Projects: Arena Circus
Tumblr: minyanstudios
itch: harlevin
Contact:

Re: Problem with renpy.random [solved]

#5 Post by minyan »

Alex wrote:Hm, it should work...
How's your code different from that sample?

Code: Select all

screen bttn_scr():
    textbutton "Press Me" action Jump("my_label") align (0.5,0.05)
    
label my_label:
    $ var = renpy.random.randint(1,3)
    if var == 1:
        "...[var]..."
    elif var == 2:
        "___[var]___"
    else:
        "~~~[var]~~~"
    jump start
    
label start:
    scene black
    show screen bttn_scr
    "..."
    "?"

After testing it some more times, I found it was working properly! I think the issue was I was trying to get it to make a new integer just by using rollback rather than leaving the scenario and coming back again like players would actually do in the game. Thank you!
ImageImage

Post Reply

Who is online

Users browsing this forum: No registered users