Skip to content
M2O Scripting APIScripting reference
Data shapeServer APIGenerated

StateBag

Defined in: server/api.d.ts:4166

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.

get(key): any

Defined in: server/api.d.ts:4172

Reads one key from this entity’s state.

string

Key to read.

any

The stored value, or undefined when the key is not set.


has(key): boolean

Defined in: server/api.d.ts:4179

Checks whether this entity’s state holds a key.

string

Key to test.

boolean

True when the key is set.


keys(): string[]

Defined in: server/api.d.ts:4185

Lists the keys this entity’s state holds, sorted.

string[]

Every key currently set, in ascending order.


onChange(key, handler): Function

Defined in: server/api.d.ts:4199

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.

string | null

Only report this key, or null to report every key of this entity.

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.

Function

A zero-argument function that cancels this subscription.


remove(key): boolean

Defined in: server/api.d.ts:4215

Removes one key from this entity’s state, telling every client that held it.

string

Key to drop.

boolean

True when the key was set and has been removed.


set(key, value, options?): boolean

Defined in: server/api.d.ts:4208

Writes one key of this entity’s state and replicates it to whoever the scope names. Throws when a limit is reached.

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.

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.

"broadcast" | "owner" | "server"

boolean

True when the value changed, false when it was already stored.


toObject(): Record<string, any>

Defined in: server/api.d.ts:4191

Copies this entity’s whole state into a plain object.

Record<string, any>

Every key and value currently set. The copy does not track later changes.