Problem with Music, Bug? [SOLVED]

Hey guys, as i was creating a game i added a music track. The problem is that when i change scenes, using the “Pause and start a New scene” and then go back to the scene using “Stop and go back to previous scene” The music stops. It just doesn’t continue. I tried multiple actions such as pause and un pause, but they didn’t seem to work. How i would like the music to function: When i change to the 2nd Scene, the music should pause (Not stop) and when i go back to 1st scene the music should continue. So, guys, any help?

I would think that the music is paused with the scene, but if it doesn’t work, as you said, you’ll have to save the offset manually :neutral_face:
There is an expression to get the music playing offset, and an action to set the offset too. When you pause the scene A and go to scene B you can save the music offset in a variable on scene A, as it’s a scene variable it’ll be saved in A because A is paused. Then you go to B, when you return to A you can set the music playing offset = the saved variable.
A numeric variable is 0 by default, so the first time you load the scene A the offset will be 0, and the music will play from the beginning, no problem here:

[code]Conditions: At the beginning of the scene
Actions: Play the music on channel 0
Set the playing music offset = Variable(music_offset)

Conditions: Conditions to go to B
Actions: Do = MusicChannelPlayingOffset(0) to the variable “music_offset”
Pause this scene and go to scene “B”[/code]
Not sure if the “At the beginning of the scene” is launched again if the scene has been unpaused, if it isn’t launched you’ll have to add a variable that works every time the scene is unpaused:

[code]Conditions: Variable “unpaused” is = 1
Actions: Play the music on channel 0
Set the playing music offset = Variable(music_offset)
Do = 0 to the variable “unpaused”

Conditions: Conditions to go to B
Actions: Do = MusicChannelPlayingOffset(0) to the variable “music_offset”
Do = 1 to the variable “unpaused”
Pause this scene and go to scene “B”[/code]

Hey! Thanks for the tip, but i still didn’t solve my issue as i cannot seem to find an action called: Do = MusicChannelPlayingOffset(0) to the variable “music_offset” (Sorry, I’m new to this stuff)

It can be a problem when the wording of the events doesn’t make it clear where the exact action can be found. In this case a scene variable is being altered, so you need to go to

Add an action: Variables>Value of a variable

and type music_offset in the Variable: box and then type MusicChannelPlayingOffset(0) in the Value: box.

This assumes you have already created a scene variable called ‘music_offset’. If you haven’t created it, then you need to right-click on the name of the scene in the Project manager window and choose ‘Modify initial variables’. Click the + and then type music_offset. The variable now exists so it can then be altered in an action.

Once you’ve done all this, the event will look like what Lizard-13 typed.

Just want to note:

  • My example also assumes you are playing the music in channel 0 (zero), that’s the reason for the “0” in the expression “MusicChannelPlayingOffset(0)”
  • It isn’t necessary to create the variable in the scene variables dialog manually, but doing it lets you set an initial value and choose the variable easily in scene conditions/actions dialogs. When a condition/action refers to a variable that has not been created, GD will create it automatically and set default values (0, “”). I think it’s good to know becuase there are lots of GD examples that use uninitialized variables, this gave me problems when I started as I though the same (that you have to create the variables) :wink:

As I was typing that out I started to think that surely there must be a way to create new variables in events too, but I had never seen a “Create variable” action. I suppose it’s the same idea as with timers - there’ s no “create timer” action either, they are just magically created when you try to pause or reset one. Good to know. :smiley:

Alright, i managed to do it. Works Perfectly! Thank you guys!!

i need this on russian, bcuz i use translator :\

Please don’t resurrect an old thread, This was for GDevelop 6 years ago. The solution then may not be relevant today.