Worldstate
Based on game build 714014 | Last updated: 2026-03-03
Overview
Worldstate is a singleton component attached exclusively to TheWorld that maintains and propagates global environmental state data—including time, day/night cycles, seasons, weather conditions, and moon phases—to other entities via variable watching. It does not control world behavior directly but serves as a central data store and notification hub. When world state changes (e.g., time advances, season shifts), the component updates its internal data table and notifies registered watchers.
Usage example
-- Access the world state component on TheWorld
local worldstate = TheWorld.components.worldstate
-- Read current state
local current_phase = worldstate.data.phase
local current_season = worldstate.data.season
-- Register a watcher for seasonal changes
worldstate:AddWatcher("season", self.inst, function(self_comp, season)
print("Season changed to:", season)
end, self)
-- Get world age in cycles
local age = worldstate:GetWorldAge()
Dependencies & tags
Components used: None identified
Tags: None identified (does not add/remove tags on itself or other entities)
Properties
| Property | Type | Default Value | Description |
|---|---|---|---|
data | table | {} | Internal dictionary storing all world state variables (e.g., time, phase, season, temperature). |
inst | entity | TheWorld | Reference to the world entity this component manages. |
Main functions
GetWorldAge()
- Description: Returns the total world age in game cycles plus fractional time.
- Parameters: None.
- Returns:
number— the number of completed cycles + current fractional time (i.e.,1 + cycles + time). - Error states: None.
AddWatcher(var, inst, fn, target)
- Description: Registers a callback function to be invoked whenever the specified world state variable changes.
- Parameters:
var(string) — name of the variable to watch (e.g.,"season","israining","isday").
inst(entity) — the entity that owns the watcher callback.
fn(function) — the callback function to invoke on change; signature:fn(target, newValue).
target(any) — optional context passed to the callback as the first argument. - Returns: Nothing.
- Error states: None. Multiple watchers per variable/entity are supported.
RemoveWatcher(var, inst, fn, target)
- Description: Removes a previously registered watcher callback.
- Parameters:
var(string) — name of the variable the watcher was registered for.
inst(entity) — the entity that registered the watcher.
fn(function ornil) — the specific callback function to remove. Ifnil, all callbacks forinston this variable are removed.
target(any ornil) — optional context; only removes matches wheretarget == v[2]. Ignored iffnisnil. - Returns: Nothing.
- Error states: No-op if watcher not found.
OnSave()
- Description: Returns a copy of the current world state data table for persistence.
- Parameters: None.
- Returns:
table— shallow copy ofself.data. - Error states: None.
OnLoad(data)
- Description: Restores world state data from a saved state table.
- Parameters:
data(table) — the saved state dictionary. Only entries matching existing keys inself.dataare applied. - Returns: Nothing.
- Error states: Ignores keys not present in
self.data.
Dump()
- Description: Returns a formatted string of all current world state variables for debugging.
- Parameters: None.
- Returns:
string— multi-line tab-separated output ofkeyandvalue. - Error states: None.
Events & listeners
- Listens to:
clocktick— updatestime,timeinphase, andcycles.
cycleschanged— updatescycles.
phasechanged/cavephasechanged— updatesphase,isday/isdusk/isnight, and related booleans. Also triggers cave-specific logic when appropriate.
moonphasechanged2/cavemoonphasechanged2— updatesmoonphase,iswaxingmoon,isfullmoon,isnewmoon, and cave equivalents.
ms_stormchanged— updatesisalterawakeon moonstorm state change.
nightmareclocktick/nightmarephasechanged— updatesnightmarephase,nightmaretime, and related booleans (isnightmarecalm, etc.).
seasontick— updatesseason, day counts, progress, and length fields.
seasonlengthschanged— updatesspringlength,summerlength, etc.
temperaturetick— updatestemperature.
weathertick— updatesmoisture,precipitationrate,snowlevel,lunarhaillevel,wetness, etc.
moistureceilchanged— updatesmoistureceil.
precipitationchanged— updatesprecipitation,israining,issnowing,islunarhailing,isacidraining.
snowcoveredchanged— updatesissnowcoveredand triggersTheSim:HandleAllSnowSymbols(show).
wetchanged— updatesiswet. - Pushes: None (no events are fired by this component).