[SOLVED] About playing a music without stopping

Started by Baguettator, Wed 05/08/2020 21:14:35

Previous topic - Next topic

Baguettator

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
// 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

Laura Hunt

#1
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
if (player.PreviousRoom != 2) valses = aValses.Play(eAudioPriorityNormal, eRepeat);


Et voilá.

Crimson Wizard

#2
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

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

if (!IsClipPlaying(aValses))
{
     aValses.Play etc...
}

Baguettator

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 !

Laura Hunt

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 :)


Crimson Wizard

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.

SMF spam blocked by CleanTalk