Ren'Py How to play music in a screen?

May 29, 2022
231
445
So I'm making a Ren'Py game and I wanted to play some music when a screen pops up. I tried using the regular "play music" statement you usually would, but it only gave an exception saying that the play music argument is not a valid argument for the screen statement. I might be wrong with remembering what it said, but it just gave an error. How do I play music in a screen? I tried googling but didn't find the answer I'm looking for.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,963
16,197
So I'm making a Ren'Py game and I wanted to play some music when a screen pops up.
Not totally sure, because I haven't had to use it, but this should works:
Python:
screen whatever():

    on "show" action Play( "music", "Whatever file you want to play" )

    [whatever you screen is]
Feature in the code:
The screen action.
The screen statement.
 
May 29, 2022
231
445
Thank you! I was able to use a $renpy.music.play statement, but the music played before the screen was even called. Maybe because I was using the "call screen" method? But the type of screen I have, you wouldn't be able to show the screen.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,963
16,197
Seems like the the "on" statement only works when a screen is shown, not called.
The on statement works even when the screen is called, and the code I gave works perfectly fine, I just tested it.
So what exceptions do you get exactly ?
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,607
2,256
To ask a dumb question... why not play the music immediately before showing/calling the screen?

I'm imagining something like:

Python:
    play music "mozart.ogg"
    call screen my_player_stats

Unless it's a screen invoked by another screen. But even then, I wonder if the music constantly restarting or switching between tracks could be a bit annoying.

Just questioning the basic premise, not suggesting you are wrong to try to do it that way.