IsActorPlayingSound

From ZDoom Wiki
Jump to navigation Jump to search


Actor

bool IsActorPlayingSound (int channel, Sound snd = -1)

Usage

Check if the calling actor is playing a sound on a specific channel.

Parameters

  • int channel
The channel ID to check if it's playing any audio. This can be one of the following:
  • CHAN_AUTO (0) — Explicitly plays the sound with the CHANF_OVERLAP flag enabled (even if it's not passed). This means that all sounds passed to this channel will play over each other, never interrupting one another. Before GZDoom 4.4.0 CHAN_AUTO used to pick the first unoccupied channel, but this behavior was dropped when the ability to play multiple sounds on the same channel was added.
  • CHAN_WEAPON (1)
  • CHAN_VOICE (2)
  • CHAN_ITEM (3)
  • CHAN_BODY (4) — The default for A_StartSound/A_PlaySound, for historical reasons.
  • CHAN_5 (5)
  • CHAN_6 (6)
  • CHAN_7 (7)
Also, any other numeric channel is supported as well.
  • Sound snd
The SNDINFO name of the sound sound to check for. If this is -1 (which is the default value), the function will check if any sound is playing on the specified channel.

Return value

  • bool — Returns true if the sound is playing on the given channel.

Examples

This is an example of a custom monster that has a charging attack, and for the first few tics of its attack animation it plays a looped sound. Normally this sound is stopped with a A_StopSound call further down the Missile state sequence, but note that the monster's attack can be interrupted before that (for example, if the monster is killed or its Pain state is triggered). With this in mind, it has a Tick() override where the looped sound will be stopped if it's currently playing but the monster is dead or not in its Missile state anymore:

class MyMonster : Actor
{
	... //omitted for brevity

	override void Tick()
	{
		Super.Tick();
		// If the looped sound is playing, but the monster is dead
		// or not in its Missile state sequence anymore, stop the
		// looped sound:
		if (IsActorPlayingSound(CHAN_BODY,"mymonster/charge/loop") && (health <= 0 || !InStateSequence(curstate, MissileState)))
		{
			A_StopSound(CHAN_BODY);
		}
	}

	States {
	Missile:
		TNT1 A 0 A_StartSound("mymonster/charge/loop", flags:CHANF_LOOPING);
		SPRT AAAAAAAA 1 A_FaceTarget();
		SPRT B 5 
		{
			A_StopSound(CHAN_BODY);
			A_FireProjectile("BFGBall");
		}
		SPRT CDE 7;
		goto See;
	}
}

(See also InStateSequence)