Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Baguettator on Wed 05/08/2020 21:14:35

Title: [SOLVED] About playing a music without stopping
Post by: Baguettator on Wed 05/08/2020 21:14:35
Hi !

I am trying to do something that I can't manage to do, about Audiochannel that I have never used before, and certainly I don't understand well how it works.

My situation is that when I enter the room 1, a song starts playing. Like :

Code (ags) Select
// this code is in room1 script
room before fadein
{
aValses.Play();
}


I have a hotspot that can make the player character to go to room 2. Okay, it works. The aValses is still playing because I didn't write anything to stop it, and that's wonderful.

In the room 2 I have a hotspot that makes the player character to go to room 1. And when I come back in room 1, the aValses is stopping and then restarting from the beginning, so we hear an interruption when the room changes, and the music restarts from the beginning.

What I want is that when I enter the room 1 (from other rooms but NOT from room 2), the music "Valses" starts to play, but if I change from room 2 to room 1, I want the music to continue and NOT to restart. I hope I'm clear, because, I'm not english :S

By reading the manual, I tried some things using Audiochannel *valse.IsPlaying, I correctly used the syntax but it didn't manage to do what I want in the game. I am almost sure that it is something like that, but I don't know how to do it correctly. And maybe it is another thing...

Does someone know how to script that situation correctly ?

Many thanks ! :D
Title: Re: About playing a music without stopping
Post by: Laura Hunt on Wed 05/08/2020 21:30:26
1- Go to the Global Variables panel, and create a new variable of the type Audiochannel. Call it "valses", for example.

2- In your room load, instead of aValses.Play(), do this:

Code (ags) Select
if (player.PreviousRoom != 2) valses = aValses.Play(eAudioPriorityNormal, eRepeat);

Et voilá.
Title: Re: About playing a music without stopping
Post by: Crimson Wizard on Wed 05/08/2020 21:42:50
If all you need is to test whether certain audio clip is playing, there's also more universal way that does not require to create global AudioChannel variables (and slightly safer because of that):

Code (ags) Select

bool IsClipPlaying(AudioClip* clip)
{
     for (int i = 0; i < System.AudioChannelCount; i++)
     {
          if (System.AudioChannels[i].PlayingClip == clip)
              return true;
     }
     return false;
}


Usage:
Code (ags) Select

if (!IsClipPlaying(aValses))
{
     aValses.Play etc...
}
Title: Re: About playing a music without stopping
Post by: Baguettator on Wed 05/08/2020 22:14:17
Awesome, I tried what Crimson Wizard explains and it works so well !

Effectively, no use of Global variables. Laura Hunts learns something too :)

Thanks to both of you !
Title: Re: About playing a music without stopping
Post by: Laura Hunt on Thu 06/08/2020 05:25:17
Quote from: Baguettator on Wed 05/08/2020 22:14:17
Awesome, I tried what Crimson Wizard explains and it works so well !

Effectively, no use of Global variables. Laura Hunts learns something too :)

Different horses for different courses. I use Audiochannel pointers for the majority of my audio scripting for various reasons, so CW's solution would not be the most suitable one for me. But it works for you, and that's great. Happy your problem is solved either way :)
Title: Re: About playing a music without stopping
Post by: Khris on Thu 06/08/2020 07:52:24
Here's my take on this: https://www.adventuregamestudio.co.uk/forums/index.php?topic=50363.msg
Title: Re: [SOLVED] About playing a music without stopping
Post by: Crimson Wizard on Thu 06/08/2020 11:08:53
On second thought, maybe I was absent minded when replying previously. If you have strictly 1 reserved channel for music, then Laura's and Khris's solution works well and safe too.