Offset on sounds api

This commit is contained in:
2026-11-22 08:35:54 -03:00
parent d18fe19027
commit 5d374091cd
5 changed files with 58 additions and 4 deletions
+20 -1
View File
@@ -138,6 +138,7 @@ With nothing but its id it's heard as it is wherever the player is, like a click
| `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:
@@ -185,4 +186,22 @@ ctx.render.register_entity_animator("copper_tools:walker", (walker) => {
});
```
A player who comes near after it started hears it too, since the animator gets the data when it's made.
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`:
```ts
// 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.