Scheduled ticks !!

This commit is contained in:
2026-10-20 13:50:35 -03:00
parent 77cd327f94
commit 1c2dcd0dbc
7 changed files with 444 additions and 1 deletions
+30
View File
@@ -70,6 +70,7 @@ component.
| `on_new_day(block, params, day)` | Every morning at 06:00. See [Ticking](#ticking) for where. | |
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. See [Changes](#changes). | |
| `on_state_change(block, params, before)` | Its own states change without it being replaced, by `set_state` or a rule. See [Changes](#changes). | |
| `on_scheduled_tick(block, params)` | A tick it asked for comes. See [Scheduled Ticks](#scheduled-ticks). | |
`block` is `{ id, x, y, z, biome, season, data, get_client_data, set_client_data, set_ticking }`, where `data` is the
block's [tile data](tiledata.md) and the client data functions its [part clients see](tiledata.md#client-data), `biome`
@@ -235,6 +236,35 @@ of a player, like the blocks Minecraft random ticks. Blocks one of them places t
block taller, wait for the next one. When time jumps forward past several mornings, like with `/time add`, each of them
runs, up to the last 64.
### Scheduled Ticks
A block that needs to do something a few ticks from now, like a button popping back out or a signal passing on after a
delay, asks for a tick instead of ticking all the time, like NeoForge's `scheduleTick`. It costs nothing while it waits,
and the block needs no tile data:
```ts
ctx.components.register_block("copper_tools:button", {
on_interact(block) {
ctx.world.set_state(block.x, block.y, block.z, "pressed", 1);
ctx.world.schedule_tick(block.x, block.y, block.z, 20);
return true;
},
on_scheduled_tick(block) {
ctx.world.set_state(block.x, block.y, block.z, "pressed", 0);
},
});
```
`ctx.world.schedule_tick(x, y, z, delay, priority?)` runs the block's `on_scheduled_tick` in `delay` ticks, at least 1.
A block has one at a time: scheduling while one waits does nothing, and `ctx.world.has_scheduled_tick(x, y, z)` says
whether one does. Its own `on_scheduled_tick` can schedule the next one, like a clock. Of the ticks due on the same
tick, the lower `priority` runs first, from -8 to 7 and 0 when not given, then the one scheduled first. If another kind
of block has taken its place by then, the tick is dropped.
Scheduled ticks run within 6 chunks of a player, like `on_tick`. Farther away they wait, and run as soon as a player
comes near. They're saved with the world, with how many ticks each had left, so they pick up where they were. At most
65,536 run in one tick, and the rest run first in the next.
The server runs a fixed 20 ticks a second. When a tick takes too long, up to ten of the next ones run back to back to
catch up. If it's further behind than that, it skips the rest and logs a warning. `/tps` shows how it's keeping up.