How to change the music that plays on the 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
Byleyg
Newbie
Posts: 11
Joined: Mon Aug 06, 2018 2:38 pm
Contact:

How to change the music that plays on the main menu?

#1 Post by Byleyg »

Hello there! Well I'm trying to make ren'py to use a different song in the main menu after getting a $ persistent.ending, I tried changing the options.rpy using

Code: Select all

define config.main_menu_music = "music/main_menu.ogg"
if persistent.ending == "ending1":
	define config.main_menu_music = "music/main_menu1.ogg"
elif persistent.ending == "ending2":
	define config.main_menu_music = "music/main_menu2.ogg"
But I don't know why but even if I delete the persistent data the second song always plays. FYI, I'm using this on the script.rpy

Code: Select all

screen main_menu:
    tag menu
    
    if persistent.ending == "ending1":
        use main_menu_ending1
This just changes the background picture of the main menu. Someone told me that if I add some code (like add "music/something.mp3") in the screen.rpy (where I define every main menu) it would also change the music that plays. Also, I'm using $ persistent.flags (along the $ persistent.ending) to change the main menu background every now and then. Maybe that's what causing the whole problem? Hopefully someone could help me, thanks :)

User avatar
TheSweetestNightmare
Regular
Posts: 43
Joined: Sat May 04, 2013 6:34 pm
Completed: None
Projects: The Catalyst(Failed Attempt), Heart of the Ocean
Location: U.S.A.
Contact:

Re: How to change the music that plays on the main menu?

#2 Post by TheSweetestNightmare »

Maybe you could use a variable in place of the song
$ persistent.main_menu_music = "music/main_menu1.ogg"
define config.main_menu_music = [persistent.main_menu_music]
Then change the persistent variable when the player reaches an ending with
label ending_1:
$ persistent.main_menu_music = "music/main_menu1.ogg"

label ending_2:
$ persistent.main_menu_music = "music/main_menu2.ogg"
Would that work?
Image
Image
-Sincerely, Faith

Byleyg
Newbie
Posts: 11
Joined: Mon Aug 06, 2018 2:38 pm
Contact:

Re: How to change the music that plays on the main menu?

#3 Post by Byleyg »

Wow, It did work. The only thing it's that it doesn't change the song with return, but using $ renpy.quit (relaunch="true") Maybe there's a way to do it without using relaunch?

Byleyg
Newbie
Posts: 11
Joined: Mon Aug 06, 2018 2:38 pm
Contact:

Re: How to change the music that plays on the main menu?

#4 Post by Byleyg »

Hmmm, I tried to use it on my actual project (I first tried on a new one) and it gives me this error, even though it works fine on the new one.

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "renpy/common/00start.rpy", line 234, in script
    $ renpy.music.play(config.main_menu_music, if_changed=True)
  File "renpy/common/00start.rpy", line 234, in <module>
    $ renpy.music.play(config.main_menu_music, if_changed=True)
TypeError: expected string or buffer

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "renpy/common/00start.rpy", line 234, in script
    $ renpy.music.play(config.main_menu_music, if_changed=True)
  File "C:\Ren'py\renpy-7.0.0-sdk\renpy\ast.py", line 862, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Ren'py\renpy-7.0.0-sdk\renpy\python.py", line 1912, in py_exec_bytecode
    exec bytecode in globals, locals
  File "renpy/common/00start.rpy", line 234, in <module>
    $ renpy.music.play(config.main_menu_music, if_changed=True)
  File "C:\Ren'py\renpy-7.0.0-sdk\renpy\audio\music.py", line 112, in play
    c.enqueue(filenames, loop=loop, synchro_start=synchro_start, fadein=fadein, tight=tight, loop_only=loop_only)
  File "C:\Ren'py\renpy-7.0.0-sdk\renpy\audio\audio.py", line 534, in enqueue
    filename, _, _ = self.split_filename(filename, False)
  File "C:\Ren'py\renpy-7.0.0-sdk\renpy\audio\audio.py", line 295, in split_filename
    m = re.match(r'<(.*)>(.*)', filename)
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/re.py", line 141, in match
TypeError: expected string or buffer

Windows-7-6.1.7601-SP1
Ren'Py 7.0.0.196
El Retorno al Charco 1.0
Sun Aug 12 00:47:56 2018

User avatar
IrinaLazareva
Veteran
Posts: 399
Joined: Wed Jun 08, 2016 1:49 pm
Projects: Legacy
Organization: SunShI
Location: St.Petersburg, Russia
Contact:

Re: How to change the music that plays on the main menu?

#5 Post by IrinaLazareva »

Code: Select all

init-1 python:
    persistent.ending=["music/main_menu.ogg"]
define config.main_menu_music = persistent.ending

label start:
    '<....>'
    
    menu:
        'end?'
        "1 end":
            $ persistent.ending[0] = "music/main_menu1.ogg"
        "2 end":
            $ persistent.ending[0] = "music/main_menu2.ogg"

    return

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 change the music that plays on the main menu?

#6 Post by MaydohMaydoh »

There's a couple of ways you could force it to play a different song, one being the use of the before_main_menu label.

Code: Select all

define config.main_menu_music = "music/main_menu.ogg"

label before_main_menu:
	if persistent.ending == "ending1":
		play music "music/main_menu1.ogg"
	elif persistent.ending == "ending2":
		play music "music/main_menu2.ogg"
Another way would be to play each one on the new screens using on 'show'

Code: Select all

screen main_menu():
	if persistent.ending == "ending1":
        	use main_menu_ending1

screen main_menu_ending1():
	on 'show' action Play("music", "music/main_menu1.ogg")

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

Re: How to change the music that plays on the main menu?

#7 Post by Imperf3kt »

Byleyg wrote: Sun Aug 12, 2018 1:30 am Wow, It did work. The only thing it's that it doesn't change the song with return, but using $ renpy.quit (relaunch="true") Maybe there's a way to do it without using relaunch?
I'd have expected it to throw an error as defines shouldn't be used like that.

In any case, to avoid relaunching the game for the changes to take effect, you could use renpy.full_restart function.
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

Byleyg
Newbie
Posts: 11
Joined: Mon Aug 06, 2018 2:38 pm
Contact:

Re: How to change the music that plays on the main menu?

#8 Post by Byleyg »

Thanks everyone, it finally worked. Since I've already had the main_menu variants in screens.rpy I simply put

Code: Select all

on 'show' action Play("music", "music/main_menu1.ogg")
It was the easiest one for me :) And it work just fine. Also, the renpy.full_restart worked perfectly. Thanks again <3

Post Reply

Who is online

Users browsing this forum: Exiscoming