Keep music from restarting.

Started by guilen, Sun 20/04/2014 10:49:05

Previous topic - Next topic

guilen

Is there a way to keep music from restarting when you re-enter a room? I use a separate room for dialogue, so having the music restart when you exit the dialog area is a real pain. I saw a previous post solving it, but the code provided didn't work. I suppose it's because of the newer version? Please help O.o for now I'm putting songs in the que, but that really doesn't help when I need a song to change immediately.

Khris

#1
The basic idea is to have a global AudioChannel* pointer and instead of simply starting the music, only to start it if the Channel isn't already playing it.

Code: ags
// add this to GlobalScript.ash
import void SetBGM(AudioClip *music);

// add this to the top of GlobalScript.asc
AudioChannel *bgm;

void SetBGM(AudioClip *music) {
  if (bgm == null || bgm.PlayingClip != music) bgm = music.Play();
}


Now simply call SetBGM(aTheMusic); in your room's before fadein event.

Edit: instead of adding room_Load to tons of rooms, one can also use on_event:
Code: ags
function on_event(EventType event, int data) {
  if (event == eEventEnterRoomBeforeFadein) {
    if (data >= 3 && data <= 9) SetBMG(aOcean);
    if (data >= 10 && data <= 14) SetBGM(aCave);
    // etc.
  }
}


Khris

You're right, I imported bgm instead of declaring it. Code should be fixed now.

Gabarts


SMF spam blocked by CleanTalk