sf2.audio
Play, stop and adjust sounds while the game runs. The sound itself comes from sf2.assets.audio or a core sound ID. Get sf2 with local sf2 = require("sf2").
sf2.audio.try_play
Section titled “sf2.audio.try_play”Start one sound owned by the current script. Returns nil, reason instead of raising when it cannot play.
Signature: sf2.audio.try_play(audio, options?)
Parameters
| Parameter | Type | Required |
|---|---|---|
audio |
AudioHandle |
Required |
options |
AudioOptions |
Optional |
Fields of AudioOptions
| Field | Type | Required | Meaning |
|---|---|---|---|
volume |
number |
Optional | Volume, 0 to 1. Default 1. |
loop |
boolean |
Optional | Default false. |
clock |
"game"|"real" |
Optional | “game” pauses with combat; “real” keeps playing. Default “game”. |
owner |
UiHandle |
Optional | A UI that owns the sound; closing the UI stops it. |
Returns: instance, or nil, reason (for example at the voice limit). A malformed argument is a Lua error.
When: Runtime callbacks after registration has finished; rejected during registration.
-- beacon came from sf2.assets.audio("audio/beacon"); hud is an open UI handle.local sound, reason = sf2.audio.try_play(beacon, { volume = 0.5, loop = true, clock = "game", owner = hud,})if not sound then sf2.log.warn(reason) endPass a handle from sf2.assets.audio, not a filename. clock = "game" pauses the sound when combat pauses; "real" keeps playing. Each mod can have 16 active sounds (64 overall); at the limit the new request fails and existing sounds are never cut off.
A sound ends when it finishes, on sf2.audio.stop, on a scene change, or when its owner UI closes. Without an owner, the end of a round or fight does not stop it, so stop it from your lifecycle callbacks. Never save instance handles.
sf2.audio.is_playing
Section titled “sf2.audio.is_playing”Ask whether one of your sounds is still active. Paused or muted sounds count.
Signature: sf2.audio.is_playing(instance)
Parameters
| Parameter | Type | Required |
|---|---|---|
instance |
AudioInstanceHandle |
Required |
Returns: Boolean; false once finished or stopped.
When: Runtime callback.
if sound and not sf2.audio.is_playing(sound) then sound = nil endsf2.audio.set_volume
Section titled “sf2.audio.set_volume”Change the volume of one of your sounds.
Signature: sf2.audio.set_volume(instance, volume)
Parameters
| Parameter | Type | Required |
|---|---|---|
instance |
AudioInstanceHandle |
Required |
volume |
number |
Required |
Returns: true if updated, false if the sound had already ended.
When: Runtime callback.
if sound then sf2.audio.set_volume(sound, 0.25) endThe value (0 to 1) multiplies the player’s saved sound volume.
sf2.audio.stop
Section titled “sf2.audio.stop”Stop one of your sounds and free its slot.
Signature: sf2.audio.stop(instance)
Parameters
| Parameter | Type | Required |
|---|---|---|
instance |
AudioInstanceHandle |
Required |
Returns: true if an active sound was stopped, otherwise false. Stopping twice is safe.
When: Runtime and lifecycle callbacks.
on_round_end = function() if sound then sf2.audio.stop(sound); sound = nil endend