game music problem [SOLVED]

Discussion in 'Programming' started by pixeltroid, Jun 12, 2019.

  1. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    467
    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: Jun 12, 2019
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,541
    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);
     
  3. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    467
    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);
     
  4. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    467
    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.
     
  5. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,541
    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.
     
  6. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    467
    hmmm.
    what might the code look like? I have 5 tracks for my game, so how should I go about this?
     
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,541
    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 likes this.
  8. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    467

    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,
     
  9. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,541
    Yes. That part wouldnt change.
     
  10. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    467
    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?
     
  11. Misty

    Misty Member

    Joined:
    Jun 22, 2016
    Posts:
    1,006
    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()
     
    pixeltroid likes this.
  12. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,541
    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 likes this.
  13. pixeltroid

    pixeltroid Member

    Joined:
    Jul 23, 2016
    Posts:
    467
    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!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice