Using a variable in a different script file
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.
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.
- 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
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.
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
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.
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.
- 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
(in script.rpy, at the beginning)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.
$ 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
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
and no other check or variable assignement. I suggest you not to use conditionals on configs, usually made to set givem values.
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
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
10 ? "RENPY"
20 GOTO 10
RUN
- 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
What exactly do you mean with "in the Main Menu Screen"?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 wayand no other check or variable assignement. I suggest you not to use conditionals on configs, usually made to set givem values.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
Re: Using a variable in a different script file
In your screens.rpy file you can see all the screens in the game, look for:
Code: Select all
screen main_menu():
- 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
When I put the code there where you said:kivik wrote: ↑Sun Jun 17, 2018 5:43 amIn your screens.rpy file you can see all the screens in the game, look for:Code: Select all
screen main_menu():
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
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.
- 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
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
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!
The doc says you add displayable that way, but I've seen people mention adding movies or sound that way - so it should work!
- 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
No, it does not work. I wonder how the people who mentioned that they added sound that way did it.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!
Re: Using a variable in a different script file
Ok so I just experimented with this, and this seems to work:
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
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
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)
- 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
It works! Thank you!kivik wrote: ↑Sun Jun 17, 2018 4:34 pm Ok so I just experimented with this, and this seems to work:
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.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
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)
Who is online
Users browsing this forum: Bing [Bot]