[SOLVED] Globally defining an AudioChannel for .volume/.play calls

Started by SpacePirateCaine, Tue 04/02/2014 09:40:31

Previous topic - Next topic

SpacePirateCaine

Hi all,

I'm currently working on background music for my game, and have run into some small snags in the way that AGS handles background music. I've managed to find a method to keep music from restarting upon room entry thanks to a thread I found from June of 2011, and was hoping that I could make use of that system globally somehow.

Khris was the one who originally proposed using this, so he'll likely recognize the following code, but what I'm attempting to use is:

Code: ags

AudioChannel*bgm_channel;
void SetBGM(AudioClip*bgm) {
  if (bgm_channel != null && bgm_channel.PlayingClip == bgm) return;
  bgm_channel = bgm.Play(eAudioPriorityHigh, eRepeat);
}

which allows me to set music to my BGM channel by room using SetBGM(aWhatever); and change volume with bgm_channel.volume = whatever while also having the added benefit that it doesn't reset the music every time the player enters the room.

I have attempted pasting the same set to the top of individual room scripts, but if I want to do a volume change in any room aside from the room it triggered, I need to call another SetBGM, which in a new room resets the music and essentially voids the entire point of the above script. I'd love if I could use that globally, but so far I haven't found anywhere in the GlobalScript that seems to work, and I don't have a very good grasp yet on import/exporting of functions. I'd be very grageful if someone could let me know where I could set it or if there's a better way to accomplish this.
Check out MonstroCity! | Level 0 NPCs on YouTube! | Life's far too short to be pessimistic.

monkey0506

Using the setup you already have, the import/export should look like this:

Code: ags
// GlobalScript.ash
import AudioChannel *bgm_channel;
import void SetBGM(AudioClip *bgm);


Code: ags
// GlobalScript.asc
AudioChannel *bgm_channel;
export bgm_channel;

void SetBGM(AudioClip *bgm)
{
  if ((bgm_channel != null) && (bgm_channel.PlayingClip == bgm)) return;
  if (bgm == null) bgm_channel = null; // added this so you can call SetBGM(null) to disable background music
  else bgm_channel = bgm.Play(eAudioPriorityHigh, eRepeat);
}


Note that the AudioChannel* has to be exported, the function does not.

Crimson Wizard

Also you can create a global variable on "Global Variables" pane.

SpacePirateCaine

Thank you, Monkey! I played around with import and export a bit, but I was getting the syntax all wrong and not quite understanding what was wrong about it. I'll be sure to keep that in mind for future operations I need to call globally. Also thanks, Crimson Wizard: I actually am using quite a few global variables to set flags for completed objectives and what-have-you, but I wasn't quite sure what I would need to set here, specifically.

Thank you once again.
Check out MonstroCity! | Level 0 NPCs on YouTube! | Life's far too short to be pessimistic.

SMF spam blocked by CleanTalk