Ren'Py Randomising labels

Malachite1789

Member
Dec 25, 2019
177
260
OK so basically i want to randomise the scene the player gets when they go through a door based on their choices. I have a piece of the puzzle already

$ randomnum = renpy.random.randint(1,2) # (randomize between 1 and 2)
if randomnum==1:

jump manbiman1
elif randomnum==2:

jump manbiwoman1

but this only randomises it at the start of a new game. Is there a way to randomise it when they choose to open the door?
 

cisco_donovan

Member
Game Developer
Sep 18, 2020
218
286
Well, um, you just have to run that code whenever you want the player to go through the door.

I mean it's the absolute fundamentals of renpy so probably you just need to read the official docs for a bit so that you understand what's happening.

Start with a label which, when you jump to it, picks a random door.

Code:
label random_jump:
    $ randomnum = renpy.random.randint(1,2) # (randomize between 1 and 2)
    if randomnum==1:
        jump manbiman1
    elif randomnum==2:
        jump manbiwoman1
Then jump to that label whenever you need.

Probably you'll have a menu somewhere. Here's an example of one at the start of the game:
Code:
label start:
    menu:
        "Go through door":
                jump random_jump
        "Go for an ice cream instead":
                jump eat_icecream
 
  • Like
Reactions: Malachite1789

Bev_

Member
Nov 17, 2018
477
776
If what you meant is, that value doesn't change when you rollback, you can try this:
Code:
        python:
            import random
        $ randomnum = random.randint(1,2)
Edit: I forgot to delete "renpy." that was the whole point of it :|
 
Last edited:
  • Like
Reactions: Malachite1789

Malachite1789

Member
Dec 25, 2019
177
260
yeah i think i get that. I also left out the If, elif chain at the startof the event after the door menu which may make a difference (It's a long change of If elif's based on what gender and preference the player chooses.) Thankyou both :)
 

Malachite1789

Member
Dec 25, 2019
177
260
If what you meant is, that value doesn't change when you rollback, you can try this:
Code:
        python:
            import random
        $ randomnum = random.randint(1,2)
Edit: I forgot to delete "renpy." that was the whole point of it :|
This was perfect Thankyou :)
 

Malachite1789

Member
Dec 25, 2019
177
260
For anyone else looking at this the problem was i had the first part

python:
import random
$ randomnum = random.randint(1,2) # (randomize between 1 and 2)

after an elif when it needed to be at the start of the label and then the next part

if randomnum==1:

jump manbiman1
elif randomnum==2:

jump manbiwoman1

needed to be after the elif(or else) that it was being used for (In my case in a string of 12 seperate elif's because i love making my life more complicated)
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,978
16,236
Code:
label random_jump:
    $ randomnum = renpy.random.randint(1,2) # (randomize between 1 and 2)
    if randomnum==1:
        jump manbiman1
    elif randomnum==2:
        jump manbiwoman1
Why so complicated ? You can do the exact same with a single line:
Code:
    jump expression "manbiman1" if renpy.randint( 1,2 ) == 1 else "manbiwoman1"
 

cisco_donovan

Member
Game Developer
Sep 18, 2020
218
286
Why so complicated ? You can do the exact same with a single line
Well your code is shorter, I'm not sure it's any less complex.

In any case it's irrelevant - this isn't an optimisation problem.

If someone is struggling with code I don't like to confuse them by re-writing the bit they've managed to get working. OP seems fairly comfortable with how they're randomly picking a branch, that's fine by me. I'd rather focus on the problem they're actually having.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,978
16,236
Well your code is shorter, I'm not sure it's any less complex.

In any case it's irrelevant - this isn't an optimisation problem.
Effectively, it's not a question of optimization, it's a question of efficient use of the language.

While it don't really matter in the present case, it become important when the randomized jump regard something like jump expression "whateverLabel_{}".format( renpy.randint(1,10) ).
Then, it's still not a question of optimization, but of limited bugs possibilities.
 
  • Like
Reactions: bobdickgus