Persistent variable and music

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
Voight-Kampff
Veteran
Posts: 351
Joined: Sat Sep 05, 2009 8:47 am
Contact:

Persistent variable and music

#1 Post by Voight-Kampff »

We have a persistent variable that we've named "mmdarkness". The long and short is, we wanted our main menu screen to slowly change color as a player progressed through the game. And it works.

Essentially, at the beginning of every scene, we declare the value of "mmdarkness". The value can range anywhere from "0" to "100", where "0" provides a nice, happy, sunny menu screen, and "100" provides a dark foreboding menu screen. A user can quit at any point, start the game back up, the persistent data is evaluated, and the appropriate color is displayed to the user.

Our problem comes from the accompanying audio. Obviously, we would like to have music that matches the mood of the title screen. Here's a simplified version of the code we're using in the options.rpy file:

Code: Select all

if persistent.mmdarkness == "0":
  config.main_menu_music = "happy.wav"
if persistent.mmdarkness == "100":
  config.main_menu_music = "sad.wav"
else:
  config.main_menu_music = "blank.wav"
This works. Sorta.

As an example, let's say the user begins a "New Game". The first scene is initialized and the value of "mmdarkness" is set to 0. At this point, the user can back out to the main menu, and the "happy" music will play. Similarly, if the user advances to a scene in which "mmdarkness" is set to 100, they can then back out to the main menu, and the "sad" music will play.

However, if the user quits at that point, and then starts back up, the main menu will play no music.

We know the "mmdarkness" file is set and is being evaluated properly on startup, otherwise we would not be displaying the proper graphics (which we are). So why is config.main_menu_music not evaluating "mmdarkness" and playing the appropriate music upon startup?

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Persistent variable and music

#2 Post by namastaii »

I'm not a pro here but it could be because config pieces like that are read in the very beginning and it doesn't come back up again (until maybe the game is restarted - idk)

Where did you put this code? Is it in the options or is it on the main menu screen itself? I think it's fine the way it's written if it were in options since it's under an init but if it's on the screen (where I believe it would work best) you'd have to use $ because it is a python statement. Eh..What I'm wondering is if you should code it a different way other than configuring the main menu music and just have music play when the menu opens but not specifically attaching it to config.main_menu_music

I noticed there were no responses so I thought I'd give my two cents in hopes it might help. I might mess around with this later today and if you still haven't figured it out and I found a solution that works, I'll share it with you.


What I'm also wondering is how well is this going to work when the player has multiple saves at different points? The variable would keep changing but I guess as long as it is one person playing the games, it wouldn't really matter.

update: Okay, I just read back at your post and realized you said it was in options.rpy so ... the if/else code for the colors and such is on the main menu screen itself right? I think that is why the colors would work and not the audio.

Voight-Kampff
Veteran
Posts: 351
Joined: Sat Sep 05, 2009 8:47 am
Contact:

Re: Persistent variable and music

#3 Post by Voight-Kampff »

Thanks.

I've tried every variation I could think of, including moving the conditional statement out of the options file and into the script file (immediately after the "label splashscreen:" to no avail. I obviously don't understand what's going on well enough to make any headway. So, I've basically given up and settled on a crummy compromise.

User avatar
namastaii
Eileen-Class Veteran
Posts: 1350
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life
Github: lunalucid
Skype: Discord: lunalucid#1991
Soundcloud: LunaLucidMusic
itch: lunalucid
Location: USA
Contact:

Re: Persistent variable and music

#4 Post by namastaii »

So I didn't use persistent in mine because I don't have a ton of experience with it so I just used a normal variable for testing purposes (hopefully it doesn't change anything)

I didn't put any if statements in the options. I added this at the top of options to define the variable really early but wherever you have yours defined is most likely fine if everything else is working like you said:

Code: Select all

init -2:
    $ mmdarkness = 1
I just put this in the main menu screen on the screens.rpy

Code: Select all

screen main_menu:
    
        if mmdarkness == 1:
            $ config.main_menu_music = "happy.mp3"
        if mmdarkness == 2:
            $ config.main_menu_music = "sad.mp3"
It seems to work for me... except the song that starts up first before playing the game at all is the sad one for some reason. I'm not sure if this helps. I was hoping it'd be more straight forward

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Persistent variable and music

#5 Post by PyTom »

What you can do is to have code:

Code: Select all

label before_main_menu:
     # This code can do whatever you want, including playing music.
     return
It's been a long time since I even knew this existed.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot], voluorem