codeAPI Reference
s&box SpeechLayer: NPC speech and subtitles
NPC behavior layer that manages speech state, plays sound files, and renders subtitle text above the NPC's head.
Properties
CSHARP
/// The subtitle text currently being shown, if any. Synced to all clients.
[Sync] public string CurrentSpeech { get; set; }
/// Whether the NPC is currently speaking.
public bool IsSpeaking => CurrentSpeech is not null;
/// Minimum seconds between speeches.
public float Cooldown { get; set; } = 8f;
/// A generic fallback sound (e.g. a grunt or mumble) played when talking without a specific sound.
public SoundEvent FallbackSound { get; set; }
/// Whether the cooldown has elapsed and the NPC can speak again.
public bool CanSpeak => _lastSpoke > Cooldown;Methods
CSHARP
/// Play a sound event and show its subtitle (if one exists) above the NPC.
public void Say( SoundEvent sound, float duration = 0f )
/// Play a sound event with an explicit subtitle override.
public void Say( SoundEvent sound, string subtitle, float duration = 0f )
/// Say a string message using the fallback sound, with the string shown as a subtitle.
public void Say( string message, float duration = 3f )
/// Stop any current speech and sound.
public void Stop()
/// Reset speech state.
public override void ResetLayer()Behavior
- Resolves a random sound file from the SoundEvent
- Uses SubtitleExtension to find subtitle text if not provided
- Syncs CurrentSpeech to all clients
- Host manages sound playback and duration
- All clients draw the subtitle overlay
- Cooldown prevents spam
Usage
Access via Npc.SpeechLayer. Call Say() with a SoundEvent or string to speak. The layer handles sound playback, subtitle display, and cooldown enforcement.
Example: A greeting task would call Say( "Hello there!" ) to show a subtitle.
Source
Used by NPC speech and dialogue systems.
Was this helpful?