[Solved] Does renpy.random.randint not work inside options.rpy?

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
vorgbardo
Regular
Posts: 31
Joined: Sun Jan 14, 2018 8:28 am
Projects: Head Case
Contact:

[Solved] Does renpy.random.randint not work inside options.rpy?

#1 Post by vorgbardo »

I'm trying to have a randomly selected audio file play while the player is at the main menu. However, for some reason I can't get the random selection to work there. The last option is always selected. Is renpy.random.randint not supposed to work inside options.rpy, or am I just making some daft mistake? The code is as follows:

Code: Select all

$ rng_main_menu_music = renpy.random.randint(1, 4)

if rng_main_menu_music > 3:
    define config.main_menu_music = "music/song1.mp3"
else:
    define config.main_menu_music = "music/song2.mp3"
Last edited by vorgbardo on Thu Jan 27, 2022 6:03 am, edited 2 times in total.
A dyslexic man walks into a bra.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Does renpy.random.randint not work inside options.rpy?

#2 Post by Ocelot »

define executes in init phase. Rest of code you wrote executes during normal script execution.
Your code is equivalent to:
Place all your code inside init block if you want it to execute during init phase.
< < insert Rick Cook quote here > >

User avatar
vorgbardo
Regular
Posts: 31
Joined: Sun Jan 14, 2018 8:28 am
Projects: Head Case
Contact:

Re: Does renpy.random.randint not work inside options.rpy?

#3 Post by vorgbardo »

Ocelot wrote: Thu Jan 27, 2022 5:05 am define executes in init phase. Rest of code you wrote executes during normal script execution.
Your code is equivalent to:
Place all your code inside init block if you want it to execute during init phase.
Thank you for the answer! Unfortunately I don't know how to do that in practice. What should be written and where to make it work? I tried the following change, but it did not fix the problem. And putting the if - else inside the init block caused an error.

Code: Select all

init python:
    rng_main_menu_music = renpy.random.randint(1, 4)

if rng_main_menu_music > 3:
    define config.main_menu_music = "music/song1.mp3"
else:
    define config.main_menu_music = "music/song2.mp3"
EDIT: Figured out what caused the error, can't have the "define" inside init block. This code works, in case someone else tries to do the same at some point:

Code: Select all

init python:
    rng_main_menu_music = renpy.random.randint(1, 4)

    if rng_main_menu_music > 3:
        config.main_menu_music = "music/song1.mp3"
    else:
        config.main_menu_music = "music/song2.mp3"
Last edited by vorgbardo on Thu Jan 27, 2022 5:54 am, edited 3 times in total.
A dyslexic man walks into a bra.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Does renpy.random.randint not work inside options.rpy?

#4 Post by Ocelot »

Now random choice is inside init block, but your conditions are not.
Right now it essentually works like that:

Code: Select all

init:
    python:
        rng_main_menu_music = renpy.random.randint(1, 4)
    define config.main_menu_music = "music/song1.mp3"
    define config.main_menu_music = "music/song2.mp3"

# Not init:
if rng_main_menu_music > 3:
    pass
else:
    pass
You want to place everything into init block.
< < insert Rick Cook quote here > >

User avatar
vorgbardo
Regular
Posts: 31
Joined: Sun Jan 14, 2018 8:28 am
Projects: Head Case
Contact:

Re: Does renpy.random.randint not work inside options.rpy?

#5 Post by vorgbardo »

Ocelot wrote: Thu Jan 27, 2022 5:43 am
Yes, got it to work, edited my reply above to show what I had missed. Thanks for the help!
A dyslexic man walks into a bra.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]