Step 4: Sound and music
Molten plays WAV and Ogg Vorbis files from assets/. There are two ways to play them: call
a function from Cast, or give an object an audioSource component.
Sounds go through four mixer buses: "master", "music", "effects", "ui". Each bus has a
volume the player can change (and that is saved in their settings).
A sound when a coin is picked up
Add audioPlayAt to the pickup behavior:
// behaviors/pickup.cast
import { audioPlayAt, behavior, placedObject, Transform } from "molten";
import { Coin } from "../components/coin";
// Collects a coin when the player walks into it.
export default behavior([Transform, Coin], (objects, dt: float): void => {
const director = placedObject("director").as<Director>()?.get();
if (!director) return;
const player = director.coinGame.player.as<Player>()?.get();
if (!player) return;
const chest = player.transform.position + vec3(0, 1, 0);
let gained = 0;
for (const obj of objects) {
if (distance(obj.transform.position, chest) < 1.2) {
gained += obj.coin.value;
audioPlayAt("audio/pickup.wav", "effects", obj.transform.position, 0.8);
obj.destroy();
}
}
// Narrowing does not carry across a loop, so test director again.
if (director && gained > 0) director.coinGame.score += gained;
});
audioPlayAt(path, bus, position, volume) plays a sound once, positioned in the world:
it is louder close to the camera and panned left or right. The other calls are:
| Call | Plays |
|---|---|
audioPlay(path, bus, volume, loop, fadeIn) |
Not positioned (music, UI clicks) |
audioPlayAt(path, bus, position, volume) |
Once, at a fixed world position |
audioPlayAttached(path, bus, object, volume) |
Following an object; stops if it is destroyed |
audioStop(voice, fadeOut) |
Stops a voice returned by one of the above |
Each play call returns a uint64 voice handle (0 means it failed).
Background music
For sounds that belong to an object, use the audioSource component. Music that should
start with the level fits well:
// objects/music.object
// Background music: starts when its level loads, on the music bus.
export default object({
audioSource: { asset: "audio/music.wav", bus: "music", volume: 0.5, loop: true, autoplay: true },
});
Place it in the chunk:
// levels/yard/chunks/0_0.chunk
export default chunk({
format: "chunk-local-v1",
coord: [0, 0],
objects: [
{ id: "ground", type: Ground },
{ id: "director", type: Director },
{ id: "music", type: Music },
{ id: "coin-1", type: Coin, position: vec3(0, 1, 6) },
{ id: "coin-2", type: Coin, position: vec3(-4, 1, -3) },
{ id: "coin-3", type: Coin, position: vec3(5, 1, -6), overrides: { coin: { value: 5 } } },
],
});
audioSource fields: asset, bus (default "effects"), volume (1), pitch (1), pan
(0), minDistance (1), maxDistance (30), loop, autoplay and spatial (all false).
With spatial: true the sound follows the object's position.
Run it
You hear music, and a chime for each coin. Replace assets/audio/pickup.wav with another
WAV while the game runs: the next pickups use the new sound, and voices already playing
restart on the new clip.
Next: physics crates.