Jibble

Author Topic: Keep music from restarting.  (Read 1195 times)

Keep music from restarting.
« on: 20 Apr 2014, 10:49 »
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

  • partook in silencing a crtitc despite facts
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on one or more games that was nominated for an AGS Award!
Re: Keep music from restarting.
« Reply #1 on: 23 Apr 2014, 21:26 »
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: Adventure Game Studio
  1. // add this to GlobalScript.ash
  2. import void SetBGM(AudioClip *music);
  3.  
  4. // add this to the top of GlobalScript.asc
  5. AudioChannel *bgm;
  6.  
  7. void SetBGM(AudioClip *music) {
  8.   if (bgm != null && bgm.PlayingClip == music) return; // don't if music is already playing
  9.   bgm = music.Play();
  10. }

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

Edit: code fixed

Edit2: shorter version:
Code: Adventure Game Studio
  1. void SetBGM(AudioClip *music) {
  2.   if (bgm == null || bgm.PlayingClip != music) bgm = music.Play();
  3. }

Edit3: instead of adding room_Load to tons of rooms, one can also use on_event:
Code: Adventure Game Studio
  1. function on_event(EventType event, int data) {
  2.   if (event == eEventEnterRoomBeforeFadein) {
  3.     if (data >= 3 && data <= 9) SetBMG(aOcean);
  4.     if (data >= 10 && data <= 14) SetBGM(aCave);
  5.     // etc.
  6.   }
  7. }
« Last Edit: 20 Dec 2018, 00:15 by Khris »
Fail at Floaty Rog' now!  still having to deal with what games are going through

Re: Keep music from restarting.
« Reply #2 on: 14 Jun 2014, 10:40 »
I got this:


Khris

  • partook in silencing a crtitc despite facts
    • Lifetime Achievement Award Winner
    • I can help with play testing
    • I can help with scripting
    • I can help with translating
    • Khris worked on one or more games that was nominated for an AGS Award!
Re: Keep music from restarting.
« Reply #3 on: 14 Jun 2014, 12:34 »
You're right, I imported bgm instead of declaring it. Code should be fixed now.
Fail at Floaty Rog' now!  still having to deal with what games are going through

Re: Keep music from restarting.
« Reply #4 on: 14 Jun 2014, 13:45 »
It's perfect now. THANKS