StateBag
Defined in: server/api.d.ts:8809
Arbitrary key/value state attached to one replicated entity, reached as entity.state. Keys set on the server replicate to every client that can currently see the entity.
Methods
Section titled “Methods”get(
key):any
Defined in: server/api.d.ts:8817
Reads one key from this entity’s state.
Parameters
Section titled “Parameters”string
Key to read.
Returns
Section titled “Returns”any
The stored value, or undefined when the key is not set.
has(
key):boolean
Defined in: server/api.d.ts:8824
Checks whether this entity’s state holds a key.
Parameters
Section titled “Parameters”string
Key to test.
Returns
Section titled “Returns”boolean
True when the key is set.
keys()
Section titled “keys()”keys():
string[]
Defined in: server/api.d.ts:8830
Lists the keys this entity’s state holds, sorted.
Returns
Section titled “Returns”string[]
Every key currently set, in ascending order.
onChange()
Section titled “onChange()”onChange(
key,handler):Function
Defined in: server/api.d.ts:8844
Watches this entity’s state. The filter is applied before the handler runs, so a listener watching one key of one entity is not woken by unrelated changes. The subscription is dropped when the registering resource stops.
Parameters
Section titled “Parameters”string | null
Only report this key, or null to report every key of this entity.
handler
Section titled “handler”Function
Called as (key: string, value: any, previous: any) with the changed key, its new value and the value before it. value is undefined when the key was removed and previous is undefined when it held nothing.
Returns
Section titled “Returns”Function
A zero-argument function that cancels this subscription.
remove()
Section titled “remove()”remove(
key):boolean
Defined in: server/api.d.ts:8860
Removes one key from this entity’s state, telling every client that held it.
Parameters
Section titled “Parameters”string
Key to drop.
Returns
Section titled “Returns”boolean
True when the key was set and has been removed.
set(
key,value,options?):boolean
Defined in: server/api.d.ts:8853
Writes one key of this entity’s state and replicates it to whoever the scope names. Throws when a limit is reached.
Parameters
Section titled “Parameters”string
Key to write; at most 64 UTF-8 bytes.
any
Value to store. Booleans, numbers and strings travel as themselves; anything else is serialized as JSON. At most 4096 bytes.
options?
Section titled “options?”Who the key reaches: every client that can see the entity (the default), only its owning client, or nobody – server-side storage that never goes on the wire.
scope?
Section titled “scope?”"server" | "broadcast" | "owner"
Returns
Section titled “Returns”boolean
True when the value changed, false when it was already stored.
toObject()
Section titled “toObject()”toObject():
Record<string,any>
Defined in: server/api.d.ts:8836
Copies this entity’s whole state into a plain object.
Returns
Section titled “Returns”Record<string, any>
Every key and value currently set. The copy does not track later changes.