Class: Networking
Provides networking utilities for state synchronization, messaging, and user session events.
For real-time network data updates, including repeated networking calls or network variable updates in an loop, an update frequency of approximately 10 to 20 Hz is recommended.
Constructors
Constructor
new Networking(): Networking;
Returns
Networking
Methods
GetNetworkID()
static GetNetworkID(): string;
Gets the network identifier for the current project session.
Returns
string
The network identifier.
IsLocalMode()
static IsLocalMode(): Promise<boolean>;
Checks whether networking is running in local mode.
Returns
Promise<boolean>
A promise that resolves to true if local mode is enabled, or
false otherwise.
IsMasterClient()
static IsMasterClient(): Promise<boolean>;
Checks whether the current client is the master client.
The master client is responsible for session-level authority and may change
during a network session. If the current master client leaves, a new master
client is automatically selected. In a local session, this method always
returns true.
Returns
Promise<boolean>
A promise that resolves to true if the current client is the
master client, or false otherwise.
GetClientId()
static GetClientId(): Promise<number>;
Gets the current client ID. Each client in a network session is assigned a unique ID that can be used for identification and session-level networking operations.
In a local session, this method alreays returns 1.
Returns
Promise<number>
A promise that resolves to the current client ID.
GetMasterClientId()
static GetMasterClientId(): Promise<number>;
Gets the master client ID.
In a local session, this method always returns 1.
Returns
Promise<number>
A promise that resolves to the master client ID.
MakeNetworkTransform()
static MakeNetworkTransform(entityHandle): Promise<boolean>;
Makes an entity use a network transform.
The transform is synchronized across clients that have joined the same network session. In a local session, this method has no effect and does not throw an error.
Parameters
| Parameter | Type | Description |
|---|---|---|
entityHandle | number | The Entity to update. |
Returns
Promise<boolean>
A promise that resolves to true if the network transform was
created, or false otherwise.
IsNetworkTransform()
static IsNetworkTransform(entityHandle): Promise<boolean>;
Checks whether an entity uses a network transform.
Parameters
| Parameter | Type | Description |
|---|---|---|
entityHandle | number | The Entity to inspect. |
Returns
Promise<boolean>
A promise that resolves to true if the entity uses a network
transform, or false otherwise.
SyncLocalTransform()
static SyncLocalTransform(entityHandle): Promise<boolean>;
Synchronizes the local transform of an entity to the network session.
The client that calls this method updates the local entity transform for all clients in the same network session. In a local session, this method has no effect and does not throw an error.
Parameters
| Parameter | Type | Description |
|---|---|---|
entityHandle | number | The Entity to synchronize. |
Returns
Promise<boolean>
A promise that resolves to true if the transform was
synchronized, or false otherwise.
NewChannel()
static NewChannel(channelName, onReceived): Promise<void>;
Creates a named message channel.
In a local session, the channel can still be created, but it does not receive messages, including messages sent by the local client.
Parameters
| Parameter | Type | Description |
|---|---|---|
channelName | string | The channel name. |
onReceived | (sender, payload) => void | The callback invoked when a message is received. |
Returns
Promise<void>
A promise that resolves when the channel has been created.
SendMessageTo()
static SendMessageTo(
playerId,
channelName,
payload): Promise<void>;
Sends a message to a specific player on a named channel.
In a local session, this method has no effect and does not throw an error.
Parameters
| Parameter | Type | Description |
|---|---|---|
playerId | number | The target player ID. |
channelName | string | The channel name. |
payload | string | The message payload. |
Returns
Promise<void>
A promise that resolves when the message has been sent.
BroadcastMessage()
static BroadcastMessage(channelName, payload): Promise<void>;
Broadcasts a message to all players on a named channel.
In a local session, this method has no effect and does not throw an error.
Parameters
| Parameter | Type | Description |
|---|---|---|
channelName | string | The channel name. |
payload | string | The message payload. |
Returns
Promise<void>
A promise that resolves when the message has been broadcast.
NewVariable()
static NewVariable<T>(initialValue, onStateChange?): NetworkState<T>;
Creates a synchronized network variable.
The returned state object holds the same value for all clients in the same
network session. Changes to value are broadcast to the session. In a local
session, the variable is still created, and the local onStateChange
callback is invoked when the state value changes.
undefined is not a valid state value. Use null instead when the state needs
to represent an intentionally empty value.
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
initialValue | NonUndefined<T> | The initial state value. Must not be undefined. |
onStateChange? | (state) => void | The callback invoked when the state value changes. It is also invoked when the initial value is first assigned to the network state. |
Returns
NetworkState<T>
The synchronized NetworkState.
NewFunction()
static NewFunction<F>(func): (...args) => void;
Creates a synchronized network function.
Calling the returned function locally broadcasts the call arguments to the network session. In a local session, the function is still created and remains callable by the local client.
Type Parameters
| Type Parameter |
|---|
F extends (...args) => any |
Parameters
| Parameter | Type | Description |
|---|---|---|
func | F | The function to synchronize. |
Returns
The synchronized function.
(...args): void;
Parameters
| Parameter | Type |
|---|---|
...args | Parameters<F> |
Returns
void
OnUserJoined()
static OnUserJoined(onUserJoined): Promise<void>;
Registers a callback for user join events.
Parameters
| Parameter | Type | Description |
|---|---|---|
onUserJoined | (user) => void | The callback invoked when a user joins. |
Returns
Promise<void>
A promise that resolves when the callback has been registered.
OnUserLeft()
static OnUserLeft(onUserLeft): Promise<void>;
Registers a callback for user leave events.
Parameters
| Parameter | Type | Description |
|---|---|---|
onUserLeft | (user) => void | The callback invoked when a user leaves. |
Returns
Promise<void>
A promise that resolves when the callback has been registered.