[Solved]Problem generating random number or using if statement... I'm not sure

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
xixikudo
Regular
Posts: 27
Joined: Wed May 30, 2018 3:07 am
Projects: WhiteNightRhapsody
Organization: NamelessElysion
Contact:

[Solved]Problem generating random number or using if statement... I'm not sure

#1 Post by xixikudo »

What I want to do is, show different main menu background randomly (one of three pictures) everytime when entering the main menu.
I tried in gui.rpy like this:

Code: Select all

## Main and Game Menus #########################################################

## The images used for the main and game menus.

$bgr = random.randint(0, 2)

if bgr == 0:
    define gui.main_menu_background = "gui/main_menu_O.png"
    jump origin

if bgr == 1:
    define gui.main_menu_background = "gui/main_menu_R.png"
    jump origin

if bgr == 2:
    define gui.main_menu_background = "gui/main_menu_B.png"
    jump origin

label origin:
##define gui.main_menu_background = "gui/main_menu.png"
define gui.game_menu_background = "gui/game_menu.png"
The game will always show the last one (main_menu_B.png) as the background.
I also tried a renpy.random.randint(0, 2) fuction, but the game won't even make it to the main menu.

I know some C/C++ grammar, but I konw nothing about Python...


Or should I look into screen.rpy?
Last edited by xixikudo on Fri Jun 01, 2018 2:41 am, edited 1 time in total.

Rayne
Newbie
Posts: 7
Joined: Mon May 28, 2018 9:46 pm
Contact:

Re: Problem generating random number or using if statement... I'm not sure

#2 Post by Rayne »

gui.main_menu_background simply defines a picture that's later added to the main menu.

Find main_menu screen in screens.rpy, delete add gui.main_menu_background and add something like this:

Code: Select all

## Main Menu screen ############################################################
##
## Used to display the main menu when Ren'Py starts.
##
## https://www.renpy.org/doc/html/screen_special.html#main-menu

screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"
    $ bgr = renpy.random.randint(0, 2)

    if bgr == 0:
        add "gui/main_menu_O.png"
    if bgr == 1:
        add "gui/main_menu_1.png"
    if bgr == 2:
        add "gui/main_menu_2.png"
That should work!

User avatar
xixikudo
Regular
Posts: 27
Joined: Wed May 30, 2018 3:07 am
Projects: WhiteNightRhapsody
Organization: NamelessElysion
Contact:

Re: Problem generating random number or using if statement... I'm not sure

#3 Post by xixikudo »

Rayne wrote: Fri Jun 01, 2018 2:34 am gui.main_menu_background simply defines a picture that's later added to the main menu.

Find main_menu screen in screens.rpy, delete add gui.main_menu_background and add something like this:

Code: Select all

## Main Menu screen ############################################################
##
## Used to display the main menu when Ren'Py starts.
##
## https://www.renpy.org/doc/html/screen_special.html#main-menu

screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"
    $ bgr = renpy.random.randint(0, 2)

    if bgr == 0:
        add "gui/main_menu_O.png"
    if bgr == 1:
        add "gui/main_menu_1.png"
    if bgr == 2:
        add "gui/main_menu_2.png"
That should work!
I see! Thanks!

Rayne
Newbie
Posts: 7
Joined: Mon May 28, 2018 9:46 pm
Contact:

Re: [Solved]Problem generating random number or using if statement... I'm not sure

#4 Post by Rayne »

Actually, here's something even better! You can replace all of these ifs with just this line:

Code: Select all

add "gui/main_menu_[bgr].png"
So it'll be:

Code: Select all

screen main_menu():

    ## This ensures that any other menu screen is replaced.
    tag menu

    style_prefix "main_menu"
    $ bgr = renpy.random.randint(0, 2)
    add "gui/main_menu_[bgr].png"

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: [Solved]Problem generating random number or using if statement... I'm not sure

#5 Post by kivik »

Just be mindful that originally xixikudo's image names have 0, B and R suffixes - which I know can be changed. However if you want to maintain the flexibility of different filenames, you can use renpy.random.choice:

Code: Select all

$ bgr = renpy.random.choice(['0', 'L', 'R'])

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]