Cache ticking blocks

This commit is contained in:
2026-10-12 19:37:04 -03:00
parent c5ffc23bcd
commit 02cbeda0a0
9 changed files with 195 additions and 56 deletions
+19 -3
View File
@@ -69,9 +69,9 @@ component.
| `on_second(block, params, dt)` | Every second, near a player. | |
| `on_new_day(block, params, day)` | Every morning at 06:00. See [Ticking](#ticking) for where. | |
`block` is `{ id, x, y, z, biome, season, data }`, where `data` is the block's [tile data](tiledata.md) and `biome` and
`season` are its column's (see [Seasons](../worldgen/biomes.md#seasons)). `params` is what the block's JSON lists for
this component, and `dt` is in seconds.
`block` is `{ id, x, y, z, biome, season, data, set_ticking }`, where `data` is the block's [tile data](tiledata.md),
`biome` and `season` are its column's (see [Seasons](../worldgen/biomes.md#seasons)) and `set_ticking` turns its
[ticking](#ticking) off and on. `params` is what the block's JSON lists for this component, and `dt` is in seconds.
## Rules
@@ -160,6 +160,22 @@ clients guess that the click places the held block and show it for a moment befo
`on_tick` and `on_second` only run for blocks that **have tile data**, within 6 chunks of a player. So a component that
ticks from the start sets `block.data` in `on_create`, like `copper_tools:oxidizes` above.
A block that only has work some of the time turns its ticking off with `block.set_ticking(false)`, like a machine with
nothing to make, so the server doesn't spend time on it every tick. It stays off, even when the world is saved and
loaded, until something turns it back on: its own `on_interact`, or any other script with
`ctx.world.set_block_ticking(x, y, z, true)`, like a pipe bringing it something to work on. Breaking or replacing the
block forgets it.
```ts
on_tick(block) {
if (!has_work(block)) {
block.set_ticking(false);
return;
}
// ...
},
```
`on_new_day` runs for every block with tile data in the whole world, near a player or not, so crops keep growing while
nobody's around. Blocks without tile data, like the cacti world generation makes, run it too, but only within 6 chunks
of a player, like the blocks Minecraft random ticks. Blocks one of them places that morning, like a cactus growing a
+2 -2
View File
@@ -27,8 +27,8 @@ Outside a handler, `ctx.world.get_block_data<T>(x, y, z)` reads it.
- Tile data is saved with the world, and goes away when the block is broken or replaced.
- It's **never sent to clients**. To show it to a player, open a [menu](../inventories/menus.md), or change a block
state that the block's `variants` show.
- Only blocks with tile data run `on_tick` and `on_second`. `on_new_day` runs for blocks without it too, but only near
players, see [Ticking](components.md#ticking).
- Only blocks with tile data run `on_tick` and `on_second`, unless they turned it off. `on_new_day` runs for blocks
without it too, but only near players, see [Ticking](components.md#ticking).
## Blocks With Containers