[Solved] How do you prevent the music and sound from stopping when starting a new game?

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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

[Solved] How do you prevent the music and sound from stopping when starting a new game?

#1 Post by henvu50 »

How do you prevent the music and sound from stopping when starting a new game?

EDIT - Okay, I figured it out with some help from discord: thanks to Seronis, Fen (Feniks) & Mr. Robot.

Note: We need to use config.main_menu_music so the music won't stop when starting a new game. But, we also need to play music based on a condition. So how do we do that? Like this:

Code: Select all

#your_rpy_file.rpy
init offset -1 # or earlier if needed
define var_some_condition = 0

init python: 
    def some_function():
        # your code here
        # return a value

    # yes, we're running code in init python
    var_some_condition = some_function()

Code: Select all

# options.rpy
# note options.rpy init offset is a default of 0, so this code runs after the code in: your_rpy_file.rpy
define config.main_menu_music = "this.ogg" if var_some_condition = 1 else "that.ogg"
EDIT - Instead of putting that if else statement inside the define config.main_menu_music, just write a function instead; thanks to advice from Axelkong.

In the future, it would be great if we didn't need to use config.main_menu_music to ensure music doesn't stop when starting a new game.
Last edited by henvu50 on Tue Jul 18, 2023 5:31 pm, edited 4 times in total.

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How do you prevent the music and sound from stopping when starting a new game?

#2 Post by henvu50 »

I found another solution. You can also register your own custom music, sfx and ambience channels like this:

Code: Select all

# early_init.rpy
init python:
    # register new audio channel
    renpy.music.register_channel(name='ambience',
                                 mixer='ambience',
                                 loop=False,
                                 stop_on_mute=True,
                                 tight=False,
                                 file_prefix=u'',
                                 file_suffix=u'',
                                 buffer_queue=True,
                                 movie=False,
                                 framedrop=True)
then play it like this

Code: Select all

# story_chap1.rpy
label storybla9384:
    play ambience audio_ambience_night
When starting a new game, the audio of the new channel won't get stopped, it will continue playing.

Post Reply

Who is online

Users browsing this forum: No registered users