10 KiB
Sounds
A mod's sounds are .ogg files in its sounds/ folder, put together into sounds by its sounds.json. Server phases,
shared rules and client phases play them with ctx.sounds.
Sound Files
Any .ogg (Vorbis or Opus) in sounds/, in folders if you like. Its path without .ogg is what sounds.json names it
by, so sounds/walker/step_1.ogg is walker/step_1. Paths are a-z, 0-9 and _.
Mono and stereo files both work, from a point in the world or not.
sounds.json
{
"format_version": 1,
"sounds": {
"walker/step": {
"files": ["walker/step_1", "walker/step_2", "walker/step_3"],
"category": "entities"
},
"walker/theme": { "files": ["walker/theme"], "category": "entities", "range": 32 },
"click": { "files": ["click"], "category": "ui", "volume": 0.5 }
}
}
Each key is a sound, whose id is <mod id>:<key>, so walker/step in copper_tools is copper_tools:walker/step. Any
mod can play any sound by its id.
| Field | What it does |
|---|---|
files |
The files it plays, one picked at random each time. A file listed twice comes up twice as often. |
volume |
0 to 1, 1 if not given. Every play multiplies it. |
pitch |
1 if not given. Every play multiplies it. |
range |
How far away it's heard at volume 1, in blocks. 16 if not given. |
category |
Which volume the player's settings play it at: music, ambient, blocks, entities, players or ui. |
A sound without a category only follows the player's master volume.
deno task check-mods reports a file that isn't there, and warns about an .ogg no sound plays.
Blocks and Entities
A block's or entity's json can name what its sounds' ids start with, and the game plays them without any code:
"sounds": "copper_tools:copper"
| For | Sound | When |
|---|---|---|
| a block | /break |
It's broken, by a player or a script's break_block. Not when a script replaces it with set_block. |
| a block | /place |
A player or a machine's use_item_on places it. |
| a block | /hit |
Every 4 ticks while a player breaks it, at a quarter of the volume, only on their client. |
| a block | /step |
Something walks on it with no step sound of its own, every 1.6 blocks, at 0.15 of the volume. |
| an entity | /hurt |
It's hurt. |
| an entity | /death |
It dies. |
| an entity | /step |
It walks on a block, every 1.6 blocks, in place of the block's step. |
| an entity | /ambient |
Every 4 to 12 seconds while it's alive, timed by each player's client. |
So "sounds": "copper_tools:copper" on a block plays copper_tools:copper/break when it breaks, and so on. Any of them
can be left out of sounds.json, and isn't played. A whole family of blocks usually shares one, like every copper
block, and check-mods reports a block or entity whose sounds has none of its own, which is usually a typo.
The player breaking or placing a block hears it right away from their own client, and everyone else from the server. Players step with the blocks they walk on, except the one playing while they sneak or fly. Anything about a mob that depends on what it's doing is its scripts' to play, see From the Server and Lasting Sounds.
Playing Sounds
Sounds are played where what makes them happens: a server phase for what happens in the world, a shared rule for what a
player's click does, a client phase for what only that player sees, like a screen. Whichever plays it, volume and
pitch multiply the sound's own, and a sound played at a volume over 1 is heard further away, not louder: at 2, twice
as far. Each throws for a sound no mod defines.
From the Server
The server only plays a sound once, for the players who can hear it then. A sound that lasts, like a machine's hum, is a client phase's to play, see Lasting Sounds.
// at a point, for every player within its range
ctx.sounds.play("copper_tools:anvil/land", x + 0.5, y, z + 0.5, { volume: 2 });
// at a block's middle
block.play_sound("copper_tools:machine/done");
// following a mob or another entity, for the players who see it within range
mob.play_sound("copper_tools:walker/growl", { pitch: 0.8 });
// only that player hears it, as it is wherever they are
player.play_sound("copper_tools:quest/complete");
ctx.sounds.play takes except, a player who doesn't get it. That's for a player whose own client already played it,
like when a client phase plays a click's sound right away and tells the server about it.
From Shared Rules
A shared rule's sound, like a door opening in its on_interact, plays right away for the player who clicked, since
their client runs the rule too, and reaches everyone else from the server:
export function shared(ctx: SharedContext) {
ctx.components.register_block("copper_tools:bell", {
on_interact(block) {
ctx.sounds.play("copper_tools:ring", block.x + 0.5, block.y + 0.5, block.z + 0.5);
return true;
},
});
}
On the server, a rule running for a player's break, place or click plays it for every player in range but that one. A
rule running for anything else, like a script changing a block, plays it for everyone. A mob opening a door changes its
states from a script, which doesn't run on_interact, so that script plays the sound itself.
On a Client
In a client phase, ctx.sounds.play plays a sound on that player's client only:
export function client(ctx: ClientContext) {
ctx.ui.add_inventory_overlay({
on_render(g) {
if (g.button("Ring", 4, 4, 40, 18)) ctx.sounds.play("copper_tools:click");
},
});
}
With nothing but its id it's heard as it is wherever the player is, like a click in a screen. The options:
| Option | What it does |
|---|---|
at |
A point it comes from. It fades to nothing at its range, and isn't played at all from further away. |
follow |
An entity's id. It comes from where the entity is drawn, and stops when the entity goes. |
loop |
Plays again from the start until stopped. |
volume |
Multiplies the sound's own. |
pitch |
Multiplies the sound's own. 2 is an octave up, and twice as fast. |
offset |
Plays as if it had started this many seconds ago. One that would have finished by now doesn't play. |
ctx.sounds.exists(id) checks a sound is there before playing it. play returns the sound playing, which can be
stopped or changed while it plays:
const hum = ctx.sounds.play("copper_tools:machine/hum", { at: { x: 10.5, y: 64.5, z: 3.5 }, loop: true });
hum.pitch = 1.5; // the machine sped up
if (!hum.playing) ctx.log("it's done");
hum.stop();
A looping sound out of range waits silently, and when the player comes back in range it plays on from as far into the file as it would have got, as if it never stopped.
What Doesn't Play
- Before the player's first click or key press. Browsers keep sound off until then. A sound played earlier is dropped, and a loop starts then.
- More than 64 at once. A new sound past that is dropped, and a loop waits for room.
Either way playing is false for a sound that never played. A file plays a moment late the first time, since it's only
decoded once something plays it.
Lasting Sounds
A sound that lasts as long as something does, like a machine's hum while it runs or a monster's music while it's in a state, goes with what the client already knows. Start it when that begins and stop it when it ends, like in an entity's animator when its client data changes:
ctx.render.register_entity_animator("copper_tools:walker", (walker) => {
let theme: PlayingSound | undefined;
return {
on_data(field) {
if (field !== "angry") return;
if (walker.get_client_data<boolean>("angry")) {
theme ??= ctx.sounds.play("copper_tools:walker/theme", { follow: walker.id, loop: true });
} else {
theme?.stop();
theme = undefined;
}
},
pose(pose, time) {/* ... */},
};
});
A player who comes near after it started hears it too, since the animator gets the data when it's made. It starts from
the beginning for them though, which is fine for a hum but not for music counting down to something. For that, the
server puts when it started in the client data too, and the animator plays it with an offset:
// server, when it gets angry
mob.set_client_data("angry_since", ctx.world.time);
// client, in on_data. world time is in ticks
const since = walker.get_client_data<number>("angry_since") ?? ctx.world.time;
theme ??= ctx.sounds.play("copper_tools:walker/theme", {
follow: walker.id,
loop: true,
offset: (ctx.world.time - since) / 20,
});
Play it as a loop even when it's longer than what it goes with: a sound that isn't a loop and is played out of range is dropped, while a loop waits for the player to come near and comes in where it would be by then.