S_GetLength

From ZDoom Wiki
Jump to navigation Jump to search


native static float S_GetLength(Sound sound_id)

Usage

Returns the length of a given sound in seconds as a float-point number. Note, this calculation may be somewhat imprecise.

This function can be called in ZScript in any context and scope.

Parameters

  • Sound sound_id
The name of a sound as defined in SNDINFO.

Return value

  • float — the length of the sound in seconds.

Examples

This is a basic example of how an NPC actor could be created with a custom function that makes them talk. Once the sound is started, the StartTalking function calculates how long it is and starts counting down a custom timer, so the NPC will stop its Talk animation when the timer runs out. (This is an example, you will have to provide your own sprites):

class TalkingNPC : Actor
{
	// Custom timer for how long this NPC
	// should talk:
	int talkTime;

	// Custom function that can be called on this
	// NPC to make them play a sound (presumably,
	// a voice line):
	void StartTalking(Sound snd)
	{
		// Calculate the duration of the sound and convert it
		// to tics by multiplying it by TICRATE:
		talkTime = int(ceil(S_GetLength(snd) * TICRATE));
		A_StartSound(snd, CHAN_VOICE); // play the sound
		SetStateLabel("Talk"); // begin talking animation
	}

	// Count the talk timer down:
	override void Tick()
	{
		Super.Tick();
		if (talkTime > 0)
		{
			talkTime--;
		}
	}

	States {
	Spawn:
		TGUY A -1;
		stop;
	Talk:
		TGUY BCDEF 1;
		#### # 0
		{
			// If the talking time has run out,
			// return to Spawn:
			if (talkTime <= 0)
			{
				return spawnstate;
			}
			// Otherwise continue talking:
			return ResolveState(null);
		}
		loop;
	}
}

(See also ResolveState and A_StartSound)