Ok sorry if anyone saw my post before I deleted it. I remembered that there's a screen-based Music Room that was more or less added in a recent Ren'Py release, so I read through that first and this one's much easier to implement than the cookbook version. Follow along with
this page from the documentation.
Here are the steps according to the documentation. The elaboration is my own.
1. Create an instance of MusicRoom. The MusicRoom constructor takes parameters to control the channel on which music is played back, and how long it takes to fade music out and back in.Following the documentation, you just have to put in this code somewhere:
Code:
init python:
# Step 1. Create a MusicRoom instance.
mr = MusicRoom(fadeout=1.0)
As long as it's in a .rpy file, it should be fine. Feel free to add it to an existing .rpy file or make a new one if it'll help with organization. Make sure you watch your indentation. The
fadeout=1.0 part is the parameter that tells "how long it takes to fade music out and back in". So the higher the number, the longer the fade/transition.
2. Add music files to the instance.Note that these should still be in the init python block and underneath previous code. So all told, it should look like this:
Code:
init python:
# Step 1. Create a MusicRoom instance.
mr = MusicRoom(fadeout=1.0)
# Step 2. Add music files.
mr.add("track1.ogg", always_unlocked=True)
mr.add("track2.ogg")
mr.add("track3.ogg")
Add all the tracks of your game here in that format. Again, watch indentation. If you want the track to always be unlocked, add that
always_unlocked=True parameter. If it's not there, then the track will only show up if the player has already heard it in the game in at least one playthrough.
3. Create a screen that uses the MusicRoom instance to create actions for buttons, imagebuttons, or hotspots. These actions can pick a track, the next or previous track, or stop and start the music.This is uh basically where you create the actual music room screen. The documentation gives a good sample code that you can use:
Code:
screen music_room:
tag menu
frame:
has vbox
# The buttons that play each track.
textbutton "Track 1" action mr.Play("track1.ogg")
textbutton "Track 2" action mr.Play("track2.ogg")
textbutton "Track 3" action mr.Play("track3.ogg")
null height 20
# Buttons that let us advance tracks.
textbutton "Next" action mr.Next()
textbutton "Previous" action mr.Previous()
null height 20
# The button that lets the user exit the music room.
textbutton "Main Menu" action ShowMenu("main_menu")
# Start the music playing on entry to the music room.
on "replace" action mr.Play()
# Restore the main menu music upon leaving.
on "replaced" action Play("music", "track1.ogg")
Personally I would place this in the screens.rpy file with all the other screen definitions, but again you can put this anywhere. And again, you should be careful with your indentation. The # notes explain pretty much what every bit of used code in this screen does. Please note that pausing is not currently supported in Ren'Py so you can only stop, play, and skip forwards or backwards a track. "Note that the actions used are members of a MusicRoom instance, so if the MusicRoom instance is named mr, then mr.Play("track1.ogg") is how you'd use the play action." If you use this exact code, you don't have to worry about this part, but if you change that very first definition, ie:
Code:
musiroo = MusicRoom(fadeout=1.0)
Then all the resulting code would be
musiroo.X instead of
mr.X, if that makes sense.
4. Add the music room screen to the main menu, or an extras menu.Well DaFool beat me to the punch while I was typing this up and pretty much nicely explained this step, so just listen to him. xD Basically you just add a button to your main_menu screen in the screens.rpy file.