API reference / @jsprismarine/entities / ComponentStates
Class: ComponentStates
Defined in: entities/src/componentState.ts:222
An entity's component state, keyed by component name.
Components use ComponentStates.get, ComponentStates.set and ComponentStates.clear with their declared key. A host uses ComponentStates.entries, ComponentStates.snapshot, ComponentStates.restore and ComponentStates.write. All of them write through ComponentStates.write, which reports each change to the owner.
Every value is frozen when it is written. A component that changes its state writes a new value; it never edits the one it read.
Constructors
Constructor
new ComponentStates(
owner):ComponentStates
Defined in: entities/src/componentState.ts:226
Parameters
owner
Returns
ComponentStates
Methods
clear()
clear<
S>(key):void
Defined in: entities/src/componentState.ts:257
Removes a component's state, which is what a finished timer leaves.
Type Parameters
S
S extends ComponentStateRecord
Parameters
key
Returns
void
Throws
When another declaration already claimed the component name.
entries()
entries():
IterableIterator<[component:string,state:ComponentStateRecord]>
Defined in: entities/src/componentState.ts:285
Every component that holds state now, with that state, in the order each was first written.
Returns
IterableIterator<[component: string, state: ComponentStateRecord]>
get()
get<
S>(key):S|undefined
Defined in: entities/src/componentState.ts:236
A component's state, or undefined when it holds none.
A stored value that fails the key's guard reads as undefined, so a component never sees a shape it did not declare.
Type Parameters
S
S extends ComponentStateRecord
Parameters
key
Returns
S | undefined
Throws
When another declaration already claimed the component name.
restore()
restore(
saved): readonlystring[]
Defined in: entities/src/componentState.ts:314
Writes a saved form back after the entity's components have initialized.
Only active components are written. A component the form leaves out keeps the state its onInitialize set, and a value that is not plain data, or fails the component's declared shape, is skipped in the same way. State declared with saved: false is never restored, so a form written before the declaration said so loads as if it left that state out.
Parameters
saved
Readonly<Record<string, unknown>>
A saved form, possibly parsed from an older or damaged record.
Returns
readonly string[]
The names of the components whose saved value was skipped as malformed.
set()
set<
S>(key,value):void
Defined in: entities/src/componentState.ts:247
Replaces a component's state.
Type Parameters
S
S extends ComponentStateRecord
Parameters
key
value
NoInfer<S>
Returns
void
Throws
When another declaration already claimed the component name.
snapshot()
snapshot():
ComponentStateSnapshot
Defined in: entities/src/componentState.ts:294
The saved form: each active component that has declared saved state, sorted by name, so two entities in the same state save the same form. State declared with saved: false is left out.
Returns
write()
write(
component,value):boolean
Defined in: entities/src/componentState.ts:272
The one write path. Every change to the slot, whether from a component, a restore or a host's undo, comes through here and is reported to the owner with its before and after values. A write of a value equal, field by field, to the one held changes nothing and reports nothing, so a component may write its state every tick without filling a log.
Parameters
component
string
The component whose state is written.
value
ComponentStateRecord | null
The new state, or null to remove it.
Returns
boolean
False, and nothing written, when the component declared a shape the value fails.