[SOLVED] Change main menu music when going back to main menu

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
elcharlo
Newbie
Posts: 4
Joined: Thu Feb 22, 2024 2:20 pm
Contact:

[SOLVED] Change main menu music when going back to main menu

#1 Post by elcharlo »

Hello!
As the title says, how do I make the main menu music change when I go back to the menu? Basically, when I launch my game, there's a music playing while the splashscreens show, and then the menu appears when the beat drops. But when I play the game and go back to the menu, the music just starts over and its less interesting. So I want to have one music file for when the game is launched, and another when the user goes back to main menu, but how do I make this work? Thanks in advance
Last edited by elcharlo on Fri Feb 23, 2024 12:08 am, edited 1 time in total.

jeffster
Miko-Class Veteran
Posts: 520
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Change main menu music when going back to main menu

#2 Post by jeffster »

Note that there are Main Menu and Game Menu. Game Menu is similar to Main Menu, only it's the one that is shown when the game is not exited. Like when you press Escape or right-click. You can set Game Menu music separately from Main Menu music.
https://renpy.org/doc/html/config.html# ... menu_music

If you are talking about changing Main Menu music indeed, then note that it's usually set with "config." variable. "config." variables are set before the game starts and aren't supposed to be changed (normally).

It is possible to set Main Menu music as a sequence of files, like this:
define config.main_menu_music = ["tune1.opus", "tune2.opus"]

Then when the first track is finished, the next one would start.
However if you come to the Main Menu, the sequence would start from the beginning, which is not what you want.

Therefore what you need is to probably not set "config.main_menu_music" at all, but start and change music with a script.
See Audio functions:
https://renpy.org/doc/html/audio.html

To start some script when Main Menu is entered, you can use callbacks.
"callback" is a function that gets called when some event happens.
For example, Ren'Py has config.context_callback.
https://renpy.org/doc/html/config.html# ... t_callback

You can set a callback like this:

Code: Select all

define config.context_callback = change_music
where "change_music" is the name of the function that would be called as a callback when contexts change.
For example:

Code: Select all

init python:
    def change_music():
        if main_menu:
            renpy.music.play("tune1.opus")
Here we defined "change_music" function that is called every time context changes (including going to Game Menu and Main Menu). First of all it checks if we are in Main Menu. (Variable "main_menu" there is True):
https://renpy.org/doc/html/store_variab ... -main_menu
Then it plays that music track.
That's how you set Main Menu music manually, not using "config.main_menu_music".

Of course you would change this function that I wrote as an example, to set a new track every time or something. (Maybe create a list of music tracks and when you want to go to the next track, check which one is played and then get the next from the list and set it playing).

If you just want to play a playlist and not reset it when exiting to Main Menu, it's even simpler.

Set playing the playlist at the "splashscreen" stage:
https://renpy.org/doc/html/splashscreen_presplash.html

and leave variables "config.main_menu_music" and "config.game_menu_music" undefined (or set them to None).

PS. Oh, sorry, I just checked, and it seems that splashscreen music would stop when starting Main Menu.
You would need to start it at stage "before_main_menu".
And you would need to set "config.main_menu_stop_channels" to an empty list, so that sound channels would not be stopped when going to Main Menu:

Code: Select all

define config.main_menu_stop_channels = []

label before_main_menu:
    play music ["track1.opus", "track2.opus"]
    return
...And this doesn't work either, because starting the game stops main menu music? I don't get it...

PPS. I'm not sure why, but this method doesn't seem to work either:

Code: Select all

init python:
    def change_music():
        if main_menu:
            renpy.music.play("track1.opus")

define config.context_callback = change_music
Sorry!
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

elcharlo
Newbie
Posts: 4
Joined: Thu Feb 22, 2024 2:20 pm
Contact:

Re: Change main menu music when going back to main menu

#3 Post by elcharlo »

Wow, thanks for your answer! Would giving you parts of my script be of any help? Sorry, I'm a complete beginner at this lol.

jeffster
Miko-Class Veteran
Posts: 520
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Change main menu music when going back to main menu

#4 Post by jeffster »

As I tried all this in actual Ren'Py program, there seems to be a problem.

If I set music playing like this:

Code: Select all

label before_main_menu:
    play music ["track1.opus", "track2.opus"]
    return
then it does play during Main Menu, but it stops when you start the game. Currently I don't know how to prevent that.

And if I set music with that config variable, it continues to play past Main Menu stage, but it can't be changed and it restarts every time from the beginning.

The callback approach doesn't play music at all, no idea why.

Probably a solution could be found by searching old forum posts or by rummaging through Ren'Py source code here:
https://github.com/renpy/renpy

At the moment I'm a bit occupied with other stuff, so I don't know when (if) i could solve this.
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

jeffster
Miko-Class Veteran
Posts: 520
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Change main menu music when going back to main menu

#5 Post by jeffster »

OK, I looked into renpy source code
https://github.com/renpy/renpy/blob/fa7 ... t.rpy#L250

and as you can see, if "config.main_menu_music" is not set, then at the "start" label music gets stopped (no idea what for).

Then this trick works to keep it playing in Main Menu and in the game without restarts and interruptions:
you set the same playlist in "config.main_menu_music" and you queue it to play (in callbacks or in "before_main_menu"):

Code: Select all

define playlist = ["track1.opus", "track2.opus"]

init python:
    # Set a variable to check if it's our first Main Menu stage:
    store.started = False

    def change_music():
        if main_menu and not store.started:

            # Now we start music only if it's that first time; otherwise let it keep playing:
            renpy.music.queue(playlist)

            # And we set that variable to show that we already started the music:
            store.started = True

# Set that function as a callback:
define config.context_callback = change_music

# Set config variable to not stop sounds entering Main Menu:
define config.main_menu_stop_channels = []

# Set "main_menu_music" playlist, mainly to avoid the music being stopped:
define config.main_menu_music = playlist

label start:
    "Game started..."
I hope it's what you wanted.

PS. All that I wrote is nonsense. The music does not stop as it is, it keeps playing between Main Menu and the game anyways; you just don't set a separate track for Game Menu.

Pardon for the confusion.
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

elcharlo
Newbie
Posts: 4
Joined: Thu Feb 22, 2024 2:20 pm
Contact:

Re: Change main menu music when going back to main menu

#6 Post by elcharlo »

Thank you very very much, it's sort of working :D. The game launches like this:

label splashscreen:
scene black
show screen noclick
play music "audio/music/Head First cut.mp3" fadein 4.0 volume 0.5
scene black with Pause(1.1)

show splash1 with dissolve
with Pause(7)

hide splash1

$ renpy.movie_cutscene("images/videos/Desirium2.webm", stop_music=False)

hide screen noclick

return


label start:


When it gets to "return", the first music I set in the playlist is the same as the one that plays during the splashscreen, so it keeps playing.

define playlist = ["audio/music/Head first cut.mp3", "audio/music/head first menu.mp3"]

But what happens is that when I go back to main menu, the first music in the playlist starts playing, which is the same as when the game launches. So to "fix" that, I removed the first song in the playlist, so when I go back to the main menu, "audio/music/head first menu.mp3" plays, which is what I want. However, when I launch the game, the music I set in the splashscreen gets cut by "audio/music/head first menu.mp3", and the transition is not smooth.

Does this have something to do with the splashscreen code? Thank you again

jeffster
Miko-Class Veteran
Posts: 520
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Change main menu music when going back to main menu

#7 Post by jeffster »

OK, I figured out how to prevent music from being stopped when the game goes past Main Menu:

It's just "music" channel that gets stopped!

If we use some other audio channel (register a custom channel, for example), then its playback won't stop.
Hence we won't need to set the config variable for Main Menu music.

Here's the solution:

Code: Select all

init python:

    # Register our custom channel
    # (under control of the volume slider "music"):

    renpy.music.register_channel("intro", "music", loop=False)

label splashscreen:

    # Play our intro music with that channel:
    play intro "music/Head First cut.mp3" fadein 4.0 volume 0.5

    # do other intro stuff...

    return

# Now set the music for Main Menu with a script
# (and NOT WITH config variable!)

label before_main_menu:

    # Check if intro music is still playing:
    $ track = renpy.music.get_playing(channel="intro")

    if not track:
        # When there's nothing playing in the "intro" channel =>
        # play some other Main Menu music, in a regular "music" channel now.
        play music "music/head first menu.mp3"

    # Otherwise just return (let the intro music continue)
    return
BTW, to post code with indentation etc, use \[code\] tags (without backslashes). (Or click the button that looks like this: </>).

And if the problem is solved, it's advisable to edit the first post and set "[SOLVED]" at the start of the title.

PS. As the code above uses "music" channel to play "repeatable" Main Menu music, that music will be stopped when leaving Main Menu. If you want it to continue into gameplay, you can use "intro" channel for that music too. In that case, detecting the intro track would be a bit different:

Code: Select all

label before_main_menu:
    # Get which track is playing:
    $ track = renpy.music.get_playing(channel="intro")

    if track != "music/Head First cut.mp3":
        # It's not the "intro" track (or nothing is playing):
        play intro "music/head first menu.mp3"

    return
If the problem is solved, please edit the original post and add [SOLVED] to the title. 8)

elcharlo
Newbie
Posts: 4
Joined: Thu Feb 22, 2024 2:20 pm
Contact:

Re: [SOLVED] Change main menu music when going back to main menu

#8 Post by elcharlo »

Thank you so much dude, this worked ! :DD

Post Reply

Who is online

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