Skip to content

Audio Scripting

Audio in Lemonate is driven by scene objects — SgAudio for global/screen sounds and SgPositionalAudio for 3D spatial audio. An SgAudioListener (usually on the camera) defines where the player hears from.

Quick start — 2D sound

Assign an audio item via a Property.Link or by placing SgAudio in the editor with the clip already linked. At runtime, create or control the player:

local SgAudio = require 'engine/sceneobjects/sgaudio'

local Sfx = Class.new(Behaviour)

Sfx:addProperty("jumpSound", Property.Link, nil, {
    allowedTypes = { "Audio" },
})

function Sfx:init()
    self.audio = await(SgAudio.create({
        audioItem = self.jumpSound,
        autoplay = false,
        loop = false,
        volume = 1,
    }))
end

function Sfx:playJump()
    self.audio:play(true)   -- force restart if already playing
end

return Sfx

If the SgAudio object is already in the scene (configured in the editor), attach a script to it and call self.node:play() directly — self.node is the SgAudio instance.

3D positional audio

local SgPositionalAudio = require 'engine/sceneobjects/sgpositionalaudio'

await(SgPositionalAudio.create({
    audioItem = self.engineSound,
    loop = true,
    volume = 0.8,
    transform = Transform.new(Vector3.new(0, 1, 0)),
}))

Place the listener on the active camera (or player) so panning and attenuation are correct.

Playback control

On SgAudio (and positional variants where supported):

audio:play(force)
audio:stop()
local playing = audio:isPlaying()
audio:setPlaybackRate(1.2)

Collections

Audio collection items can drive playlist-style sources — set sourceType, audioCollectionItem, and audioCollectionEntry on create. See SgAudio.

Overview

Editor-focused audio concepts (buses, mixing) are in Audio System.

Further reading