Using a variable in a different script file

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
Pent
Newbie
Posts: 12
Joined: Wed Apr 04, 2018 2:37 pm
Completed: Project N (Not public), Project N 2 (Not Public)
Projects: Project N, Project N 2, Project G
Contact:

Using a variable in a different script file

#1 Post 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.

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

Re: Using a variable in a different script file

#2 Post 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.

User avatar
Pent
Newbie
Posts: 12
Joined: Wed Apr 04, 2018 2:37 pm
Completed: Project N (Not public), Project N 2 (Not Public)
Projects: Project N, Project N 2, Project G
Contact:

Re: Using a variable in a different script file

#3 Post 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.

User avatar
gas
Miko-Class Veteran
Posts: 842
Joined: Mon Jan 26, 2009 7:21 pm
Contact:

Re: Using a variable in a different script file

#4 Post 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.
If you want to debate on a reply I gave to your posts, please QUOTE ME or i'll not be notified about. << now red so probably you'll see it.

10 ? "RENPY"
20 GOTO 10

RUN

User avatar
Pent
Newbie
Posts: 12
Joined: Wed Apr 04, 2018 2:37 pm
Completed: Project N (Not public), Project N 2 (Not Public)
Projects: Project N, Project N 2, Project G
Contact:

Re: Using a variable in a different script file

#5 Post 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"?

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

Re: Using a variable in a different script file

#6 Post 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():

User avatar
Pent
Newbie
Posts: 12
Joined: Wed Apr 04, 2018 2:37 pm
Completed: Project N (Not public), Project N 2 (Not Public)
Projects: Project N, Project N 2, Project G
Contact:

Re: Using a variable in a different script file

#7 Post 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.

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

Re: Using a variable in a different script file

#8 Post 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.

User avatar
Pent
Newbie
Posts: 12
Joined: Wed Apr 04, 2018 2:37 pm
Completed: Project N (Not public), Project N 2 (Not Public)
Projects: Project N, Project N 2, Project G
Contact:

Re: Using a variable in a different script file

#9 Post 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"

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

Re: Using a variable in a different script file

#10 Post 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!

User avatar
Pent
Newbie
Posts: 12
Joined: Wed Apr 04, 2018 2:37 pm
Completed: Project N (Not public), Project N 2 (Not Public)
Projects: Project N, Project N 2, Project G
Contact:

Re: Using a variable in a different script file

#11 Post 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.

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

Re: Using a variable in a different script file

#12 Post 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)

User avatar
Pent
Newbie
Posts: 12
Joined: Wed Apr 04, 2018 2:37 pm
Completed: Project N (Not public), Project N 2 (Not Public)
Projects: Project N, Project N 2, Project G
Contact:

Re: Using a variable in a different script file

#13 Post 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!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot]