Page 1 of 1

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

Posted: Thu Jan 27, 2022 3:32 am
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"

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

Posted: Thu Jan 27, 2022 5:05 am
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.

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

Posted: Thu Jan 27, 2022 5:39 am
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"

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

Posted: Thu Jan 27, 2022 5:43 am
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.

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

Posted: Thu Jan 27, 2022 5:52 am
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!