Page 1 of 1

Using a variable in a different script file

Posted: Sun May 20, 2018 2:25 pm
by Pent
Is it possible to use a variable declared in script.rpy in options.rpy?
I'd like the title screen music to change when the player beats the game once. For that, I need to declare a variable in script.rpy which is true if the player reaches the
ending, and then use that variable in a if-statement in options.rpy.

Re: Using a variable in a different script file

Posted: Sun May 20, 2018 2:51 pm
by kivik
First part to the answer is that, Renpy doesn't care about whether a variable is in script.rpy or options.rpy - you can even redeclare variables in multiple script files - wherever it's declared last - by init order, will be what determines the state of the variable once the game starts. The higher the value (later order) gets executed last and thus determines the final state.

Second part to the answer is - you need to use a persistent variable to record whether player has finished the game or not. Then when loading the music - use the persistent variable to decide what gets loaded.

Re: Using a variable in a different script file

Posted: Sat Jun 16, 2018 7:14 am
by Pent
kivik wrote: Sun May 20, 2018 2:51 pm First part to the answer is that, Renpy doesn't care about whether a variable is in script.rpy or options.rpy - you can even redeclare variables in multiple script files - wherever it's declared last - by init order, will be what determines the state of the variable once the game starts. The higher the value (later order) gets executed last and thus determines the final state.

Second part to the answer is - you need to use a persistent variable to record whether player has finished the game or not. Then when loading the music - use the persistent variable to decide what gets loaded.
(in script.rpy, at the beginning)

$ persistent.var = False
#at the end of the game
$ persistent.var = True
#in options.rpy
if not persistent.var:
define config.main_menu_music = "title_screen.ogg"
else:
define config.main_menu_music = "title_screen2.ogg"

It does not work. "title_screen2.ogg" plays every time, no matter what.

Re: Using a variable in a different script file

Posted: Sat Jun 16, 2018 9:59 am
by gas
Obviously, as you set that persistent twice, and each time renpy load the options restore to True.
Persistent have a special property, is set to None if not defined. So change your game this way

Code: Select all

# at the end of the game
$ persistent.var = True
# in the MAIN MENU SCREEN, not in configs
    if persistent.var==True:
        # do something
    else:
       # do somethintg else
and no other check or variable assignement. I suggest you not to use conditionals on configs, usually made to set givem values.

Re: Using a variable in a different script file

Posted: Sat Jun 16, 2018 4:01 pm
by Pent
gas wrote: Sat Jun 16, 2018 9:59 am Obviously, as you set that persistent twice, and each time renpy load the options restore to True.
Persistent have a special property, is set to None if not defined. So change your game this way

Code: Select all

# at the end of the game
$ persistent.var = True
# in the MAIN MENU SCREEN, not in configs
    if persistent.var==True:
        # do something
    else:
       # do somethintg else
and no other check or variable assignement. I suggest you not to use conditionals on configs, usually made to set givem values.
What exactly do you mean with "in the Main Menu Screen"?

Re: Using a variable in a different script file

Posted: Sun Jun 17, 2018 5:43 am
by kivik
Pent wrote: Sat Jun 16, 2018 4:01 pm What exactly do you mean with "in the Main Menu Screen"?
In your screens.rpy file you can see all the screens in the game, look for:

Code: Select all

screen main_menu():

Re: Using a variable in a different script file

Posted: Sun Jun 17, 2018 6:23 am
by Pent
kivik wrote: Sun Jun 17, 2018 5:43 am
Pent wrote: Sat Jun 16, 2018 4:01 pm What exactly do you mean with "in the Main Menu Screen"?
In your screens.rpy file you can see all the screens in the game, look for:

Code: Select all

screen main_menu():
When I put the code there where you said:
if not persistent.var:
define config.main_menu_music = "title_screen.ogg"
else:
define config.main_menu_music = "title_screen2.ogg"

No music plays in the main menu.

Re: Using a variable in a different script file

Posted: Sun Jun 17, 2018 7:29 am
by kivik
If you read what gas said - he said NOT to do it via the config variables, but to directly change the music on your main menu screen - i.e. add the music to that screen directly with your condition statement.

Re: Using a variable in a different script file

Posted: Sun Jun 17, 2018 9:00 am
by Pent
kivik wrote: Sun Jun 17, 2018 7:29 am If you read what gas said - he said NOT to do it via the config variables, but to directly change the music on your main menu screen - i.e. add the music to that screen directly with your condition statement.
But how do I do that? I tried out many functions, but I always get the same error:
"(function) is not a keyword argument or valid child for the screen statement"

Re: Using a variable in a different script file

Posted: Sun Jun 17, 2018 9:08 am
by kivik
I've not played with music files, but I think you just add it: https://www.renpy.org/doc/html/screens.html#add

The doc says you add displayable that way, but I've seen people mention adding movies or sound that way - so it should work!

Re: Using a variable in a different script file

Posted: Sun Jun 17, 2018 3:39 pm
by Pent
kivik wrote: Sun Jun 17, 2018 9:08 am I've not played with music files, but I think you just add it: https://www.renpy.org/doc/html/screens.html#add

The doc says you add displayable that way, but I've seen people mention adding movies or sound that way - so it should work!
No, it does not work. I wonder how the people who mentioned that they added sound that way did it.

Re: Using a variable in a different script file

Posted: Sun Jun 17, 2018 4:34 pm
by kivik
Ok so I just experimented with this, and this seems to work:

Code: Select all

screen test_menu:
    default song = "title_screen2" if persistent.var else "title_screen"
    on "show" action Play("music", "%s.ogg" % song, loop=True)
    on "hide" action Stop("music")
    # rest of your menu code
Using on "show" you can queue the Play() screen action, just have to make sure you stop the music on hide when the screen is gone as well.

Alternatively you can use a python if else statement for the entire code block - but since you're just changing what tune to play, it seems to make sense just to set the song name to a variable.

You can even ditch the use of the screen variable by doing this:

Code: Select all

on "show" action Play("music", "%s.mp3" % ("music" if persistent.fdsa else "music2"), loop=True)

Re: Using a variable in a different script file

Posted: Mon Jun 18, 2018 9:23 am
by Pent
kivik wrote: Sun Jun 17, 2018 4:34 pm Ok so I just experimented with this, and this seems to work:

Code: Select all

screen test_menu:
    default song = "title_screen2" if persistent.var else "title_screen"
    on "show" action Play("music", "%s.ogg" % song, loop=True)
    on "hide" action Stop("music")
    # rest of your menu code
Using on "show" you can queue the Play() screen action, just have to make sure you stop the music on hide when the screen is gone as well.

Alternatively you can use a python if else statement for the entire code block - but since you're just changing what tune to play, it seems to make sense just to set the song name to a variable.

You can even ditch the use of the screen variable by doing this:

Code: Select all

on "show" action Play("music", "%s.mp3" % ("music" if persistent.fdsa else "music2"), loop=True)
It works! Thank you!