How to fade music that is playing when a save is loaded?

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
Xevvy
Newbie
Posts: 5
Joined: Thu May 07, 2020 9:27 am
Contact:

How to fade music that is playing when a save is loaded?

#1 Post by Xevvy »

Hi all,

I've asked a couple of times on the discord and have yet to find a solution, so I'm posting here in the hope someone will know how to do what I'm trying to do.

The Issue
When loading a save game file, whatever music is playing, be it the main menu music or music that is playing as part of a game script, will stop suddenly and the new track will fade in.

What I Want
I want whatever music is playing when a file is loaded to fade out over 1 second when the load operation occurs, before the game music starts.

I've experimented with the after_load label. While this corrects the fadeout for the previously playing music, it also stops whatever music was supposed to be playing in the script from starting until a new play music command is reached in the script.

Does anyone know a solution to this? I wouldn't be opposed to having the load operation pause for a second to allow the fadeout to occur before actually completing the load in operation, so long as we can fade the screen to black while this is happening.

Thanks in advance! It's the only remaining issue with my main menu UI so I'm really keen to get this working as I want it :)

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to fade music that is playing when a save is loaded?

#2 Post by Imperf3kt »

If after_load doesn't do the trick, have you considered adding the action to the load button action?
You may need to give it some time to execute its behaviour before the loading actually takes place, and the order of events would be important.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Xevvy
Newbie
Posts: 5
Joined: Thu May 07, 2020 9:27 am
Contact:

Re: How to fade music that is playing when a save is loaded?

#3 Post by Xevvy »

What is the correct syntax for stopping music with a fadeout as a button action? I know you can can use Stop('music'), but I can't figure out how to pass it a fadeout argument.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to fade music that is playing when a save is loaded?

#4 Post by hell_oh_world »

Xevvy wrote: Wed May 13, 2020 9:34 pm What is the correct syntax for stopping music with a fadeout as a button action? I know you can can use Stop('music'), but I can't figure out how to pass it a fadeout argument.
probably...

Code: Select all

button action Function(renpy.musci.stop, "music", 2.0) # stops the playing music in `music` channel by fading it out for 2 seconds...
https://www.renpy.org/doc/html/audio.ht ... music.stop

Xevvy
Newbie
Posts: 5
Joined: Thu May 07, 2020 9:27 am
Contact:

Re: How to fade music that is playing when a save is loaded?

#5 Post by Xevvy »

I've added this to what I believe is the action statement responsible for the load operation, however there's no difference in behavior. I've also tried adding a pause statement to this (Function(renpy.pause, 1.0, hard=True)]), hopefully that's correct syntax, but neither seem to have any effect.

Code: Select all

grid columns rows:
            transpose True
            ypos 0.01
            xfill True
            style_group 'file_picker'
            for i in range(1, columns * rows + 1):
                button:
                    action FileAction(i)
                    xfill True
                    has hbox
                    add FileScreenshot(i)
                    $ description = '% 2s. %s\n%s' % (FileSlotName(i, (columns * rows)), FileTime(i, empty=_('Empty Slot.')), FileSaveName(i))
                    text description
                    key 'save_delete' action FileDelete(i)
This is the code I believe is responsible for the loading operation, would that be correct? In this instance, I've added the actions under action FileAction(i). Any ideas?

Something to note is it's probably very old code. I think this code was implemented in 2015, although it may have even been 2012 or 2013. Some of it may be depreciated by todays standards. The game I'm working on it quite old and is essentially going through something of a relaunch as the original fell apart.
Last edited by Xevvy on Wed May 13, 2020 10:11 pm, edited 1 time in total.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to fade music that is playing when a save is loaded?

#6 Post by hell_oh_world »

Yes, FileAction is responsible for that. Just be sure to add the Function first before the FileAction...
action [Function(), FileAction ()]

Xevvy
Newbie
Posts: 5
Joined: Thu May 07, 2020 9:27 am
Contact:

Re: How to fade music that is playing when a save is loaded?

#7 Post by Xevvy »

Code: Select all

action [Function(renpy.music.stop, "music", 2.0), Function(renpy.pause, 1.0, hard=True), FileAction(i)]
Is this the correct way and syntax to define this? Because as is it produces errors on both the load and save screens, as well as the continue button on the main menu.

Code: Select all

Exception: Cannot start an interaction in the middle of an interaction, without creating new context.
If I remove the pause, it works, but the music fadeout still doesn't occur, presumably because the command to start the music in the script overrides it, which is why I was hoping to use the pause. Is there a better way of doing this with a different command, allowing the fadeout to happen without being interrupted?

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to fade music that is playing when a save is loaded?

#8 Post by hell_oh_world »

you can't use pause on screens... just remove that. also note that, i haven't had much experience with audio in renpy, but i think your best bet is to create different channels for your main menu music and others so that you can control them with ease. i think playing a music in the same channel causes that behavior that you described.

Xevvy
Newbie
Posts: 5
Joined: Thu May 07, 2020 9:27 am
Contact:

Re: How to fade music that is playing when a save is loaded?

#9 Post by Xevvy »

Remember that it's happening with music played in scripts as well. So unless I defined every single piece of music as a different channel, it wouldn't be a solution. What a pain :(

Thank you for trying, anyway. I'll revert back for now and I think I'll just have to deal with this behavior unless someone comes along with a magic solution.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to fade music that is playing when a save is loaded?

#10 Post by Imperf3kt »

This is something I'll keep in mind.
Shame the proposed idea didn't pan out.

If I ever come across the issue myself and find a fix, I'll be sure to post it.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

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

Re: How to fade music that is playing when a save is loaded?

#11 Post by gas »

This is an impossible mission.

You can't PAUSE RENPY, what you can 'delay' is a possible user click.
The "interaction" is not the user one, is what renpy does between user clicks.
So, for example, you click a button, and that's "start an interaction" (update something, move things around) that last until a new click is required.
If you set a renpy.pause, the user can't click, but the interaction is going on anyway!
There's no way to "pause" the engine (only when crashing it does 'stop').

So, once you'll instruct renpy to load as part of an interaction, that's what renpy does. This is why introducing a pause and a load in the same interaction tell you can't have two interactions going (one for the pause and one for the load).

As using a function to determine a fadeout, the fadeout is ACTUALLY happening! But the interaction tell renpy to fadeout AND loading. Those two things can happen in the same interaction, so loading will stop the ongoing music.

The after_load label allow you to do something just a moment the game state change into the loaded one. And the state is ONLY a number of variables, not the entire game. The channel mixer isn't touched and so if you set a fadeout
the volume 0 is kept even with the new state variables taking course.

I'm sorry but what you'll try to do is actually impossible if not by a rewriting of the core.
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
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to fade music that is playing when a save is loaded?

#12 Post by Imperf3kt »

I can't accept "impossible", so I came up with this:

Edit the button found in the file_slots screen to this

Code: Select all

action Call("force_load_delay"), FileAction(slot)
Add this to your script

Code: Select all

label force_load_delay():
    stop music fadeout 2.0
    $renpy.pause(2.0, hard=True)
    return

And done.

Naturally it has some bugs that need ironing out, but it does work as intended, and with some more effort, I assume can be improved upon.

Note: don't use renpy.call_in_new_context('force_load_delay'), as you will NOT be able to close the program, you'll have to forcefully terminate it.


Nevermind, this completely ruins the behaviour of the save/load actions

I fiddled with it for an hour, and everything I managed to come up with didn't quite work.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: How to fade music that is playing when a save is loaded?

#13 Post by MaydohMaydoh »

If you save the currently playing song to the save json when you save the game and pass the save name to a persistent variable or something when loading, you could call the song to be played from the after_load label.

Code: Select all

def SaveData(d):
    d['current_music'] = renpy.music.get_playing(channel='music')
config.save_json_callbacks.append(SaveData)

screen save:
    button action [ SetField(persistent, 'save_slot', i), FileAction(i) ]
    
label after_load:
    play music FileJson(persistent.save_slot, 'current_music') fadeout 1.0 fadein 1.0
Problem is if it's saved during the same statement as a new song starts to play, and you load it, it will just go straight to playing that new song instead.

Post Reply

Who is online

Users browsing this forum: Sugar_and_rice