game music problem [SOLVED]

pixeltroid

Member
So basically I have assigned the music for each room or area in the room creation code.

Code:
audio_sound_gain(ost_1, 0, 5000);
audio_play_sound(ost_2, 8, true);
So when I enter a room, the previous rooms soundtrack fade outs and the current rooms music "ost_2" starts playing.

The music plays in the background when I go to rooms B and C. BUT when I go back to room A, the level music "ost_2" starts playing again over itself. So I'm stuck with 2 instances of the same song playing in the background.

How do I make it so that re-entering the room with "ost_2" does not cause the currently playing level music to start playing over itself?? Is there a way to tell the game to ignore the code to play "ost_2" when its already playing??

Any help would be appreciated!
 
Last edited:

TheouAegis

Member
Are you actually setting each specific sound to fade out? Don't tell it to fade out a specific sound, save the currently playing sound in a variable and fade-out that variable. Then play The sounds you want to have played, and set the variable to that sound.

Code:
audio_sound_gain(BGM, 0, 5000);
BGM = audio_play_sound(ost_2, 8, true);
 

pixeltroid

Member
Are you actually setting each specific sound to fade out? Don't tell it to fade out a specific sound, save the currently playing sound in a variable and fade-out that variable. Then play The sounds you want to have played, and set the variable to that sound.

Code:
audio_sound_gain(BGM, 0, 5000);
BGM = audio_play_sound(ost_2, 8, true);
Does that go into the room creation code? Because thats where I have the codes right now. I tried your codes and got an error:

Variable obj_player.BGM(100139, -2147483648) not set before reading it.
at gml_Room_room_T1_Create (line 1) - audio_sound_gain(BGM, 0, 5000);
 

pixeltroid

Member
Are you actually setting each specific sound to fade out? Don't tell it to fade out a specific sound, save the currently playing sound in a variable and fade-out that variable. Then play The sounds you want to have played, and set the variable to that sound.

Code:
audio_sound_gain(BGM, 0, 5000);
BGM = audio_play_sound(ost_2, 8, true);
I tried this code from the yoyo manual

Code:
var BGM = ost_2
audio_sound_gain(BGM, 0, 5000);
audio_play_sound(ost_2, 8, true);
But I'm still facing the same issue. When I return to the room with the music playing, it starts playing the same music again.
 

TheouAegis

Member
Does that go into the room creation code? Because thats where I have the codes right now. I tried your codes and got an error:

Variable obj_player.BGM(100139, -2147483648) not set before reading it.
at gml_Room_room_T1_Create (line 1) - audio_sound_gain(BGM, 0, 5000);
You need to create BGM atvthe start of the game before any rooms that play music.

Oh yeah, also make it a global variable. I forgot you are using the room settings method.

global.BGM = noone;

I think audio starts at 0, so default it to -1 or noone.
 

pixeltroid

Member
You need to create BGM atvthe start of the game before any rooms that play music.

Oh yeah, also make it a global variable. I forgot you are using the room settings method.
hmmm.
what might the code look like? I have 5 tracks for my game, so how should I go about this?
 

TheouAegis

Member
Make an empty room at the very start of your rooms list. Put

global.BGM = noone;
room_goto_next();

in the room settings for that room. Then in the code i gave you previously, just change BGM to global.BGM.
 

pixeltroid

Member
Make an empty room at the very start of your rooms list. Put

global.BGM = noone;
room_goto_next();

in the room settings for that room. Then in the code i gave you previously, just change BGM to global.BGM.

okay I have 5 tracks for my game, so... do I keep changing global.BGM to the different soundtracks in the creation codes of different rooms? im confused,
 

pixeltroid

Member
Yes. That part wouldnt change.
hey. Your code works for the most part. Its made it easier for me to keep track of level music. But there's one problem.

If Im playing "ost_2" in room A, and the next room (Room B) does not have a music assigned to it, ost_2 keeps playing in room B. Which is fine.

However,when I go from room B to room A, 2 instances of ost_2 play over each other for a while before one of them fades out.

Is there a way to prevent whats happening?
 
M

Misty

Guest
Yes. You need to check if the same sound is playing before you play it while it is playing. The function you need is audio_is_playing()
 

TheouAegis

Member
Or

if global.BGM!=ost_2
{
audio_sound_gain(BGM, 0, 5000);
audio_play_sound(ost_2, 8, true);
}

if you want the music to stop in a room that doesn't have music, then just stop the music and don't play a new one.
 

pixeltroid

Member
Yes. You need to check if the same sound is playing before you play it while it is playing. The function you need is audio_is_playing()
hey!
I added this to the code

if audio_is_playing(ost_2){
exit;
}


But I still faced the same problem of the OST playing over each other

Then I moved the
"audio is playing" code ABOVE the code for playing the music and it worked! Thanks!
 
Top