Skip to content

SgAudio

Represents a non-positional audio source in the scene graph. Used to play background music or UI sounds that are not tied to any 3D location. Ideal for ambient tracks or global sound effects.

Inherits from

SceneObject

Usage

To use this class, add the following require at the top of your script:

local SgAudio = require 'engine/sceneobjects/sgaudio'

Reference

create

static create(self, options, parent)

Create a new audio object in the scenegraph.

Parameters

  • self
  • options (table): A table of options to fill the parameters
  • parent (SceneObject): The parent scene object to add this to

Returns

  • Promise: promise which will resolve to the created object

Example

-- Options can be omitted. This example shows the defaults,
-- only specify the ones you want different.
SgAudio.create({
    active = true,
    name = "",
    tags = {},
    sourceType = "Single",
    audioItem = nil,
    audioCollectionItem = nil,
    audioCollectionEntry = "",
    autoplay = false,
    loop = false,
    loopSection = false,
    loopStart = 0,
    loopEnd = 1,
    volume = 1,
    panning = 0,
    duration = 0,
    detune = 0,
    playbackRate = 1,
    offset = 0
})

Properties

You can access these properties directly on the object or through :get("propertyName") or :set("propertyName", value) methods.

Property Type Default Description
active Boolean true Whether this audio source is enabled. Inactive sources do not play or update.
tags Tags [] Labels attached to this audio source for filtering and lookup from scripts.
sourceType Enum Single Whether this prefab plays a single Audio item or picks a random entry from an Audio Collection. | Options: Single, Collection
autoplay Boolean false Starts playback automatically when the scene loads and this node is active.
loop Boolean false Repeats playback from the beginning when the audio reaches the end.
volume Float 1 Playback loudness from 0 (silent) to 1 (full volume). Applied on top of any per-source gain settings.
panning Float 0 Stereo pan from -1 (full left) to 1 (full right). 0 is centered.
duration Float 0 Length of the audio clip in seconds. Read-only for most imported audio files.
detune Float 0 Pitch offset in cents (100 cents = one semitone). Positive values raise pitch.
playbackRate Float 1 Speed multiplier for playback. 1 is normal speed; 2 plays twice as fast.
offset Float 0 Start offset in seconds. Playback begins from this point in the clip.