1.9 KiB
Time
The server runs at a fixed 20 ticks a second, and everything in the game is measured in ticks: cooking times, burn times, timers and the time of day.
Timers
ctx.system runs code later, counted in ticks:
const handle = ctx.system.run_timeout(() => player.send_message("Five seconds later"), 5 * 20);
const every_minute = ctx.system.run_interval(() => save_stats(), 60 * 20);
ctx.system.clear_run(every_minute);
ctx.system.current_tick; // ticks since the server started
run_timeout(fn, 0) runs on the next tick, which is how a before event gets something done
after it. Timers aren't saved: they're gone after a restart, so set them up again in the server phase.
For work every tick, subscribe to the tick event. For work on a block, use a
component's on_tick or on_second.
World Time
ctx.world.time counts ticks since 06:00 of the world's first day, and never wraps around:
- A day, 06:00 to 18:00, is 36000 ticks: 30 minutes.
- A night, 18:00 to 06:00, is 12000 ticks: 10 minutes. So in game hours go by faster at night.
- Dusk is the first minute of the night and dawn the last, so it's bright all day.
ctx.world.day is the number of days since the world started, 0 on the first day, going up at 06:00. Every morning at
06:00, blocks' on_new_day runs, then the new_day event fires. That's the clock crops grow on.
ctx.world.hour is the time on a 24 hour clock, in hours: 14.5 is 14:30.
ctx.world.set_time(ticks) changes the time for everyone. Moving it forward past mornings runs each of them, up to the
last 64. Moving it back doesn't repeat mornings.
common/time.ts has clock, format_clock, is_day and daylight for turning ticks into something readable. Client
scripts can read the time as ctx.world.time.
Players can change it with /time set <day|noon|sunset|night|midnight|ticks> and /time add <ticks>.