Skip to main content

Custom network variables

Use [TransSync] when a TSMPNetworkBehaviour needs to send data every frame or whenever a value changes. The encoder reads the field, writes a variable message, and the decoder applies it to the matching receiver field.

Minimal component

using K13A.TSMP;
using K13A.TSMP.Udon;
using UnityEngine;

public class DoorStateSync : TSMPNetworkBehaviour
{
[TransSync("door.open")]
public bool isOpen;

public override void TSMPBeforeEncode()
{
isOpen = transform.localEulerAngles.y > 45f;
}

public override void OnTSMPVariableReceived()
{
transform.localRotation = Quaternion.Euler(0f, isOpen ? 90f : 0f, 0f);
}
}

[TransSync] keys

[TransSync("health.current")]
public int health;

The key is hashed into a stable variableHash. Use explicit keys instead of relying on field names when the field might be renamed later.

Supported value types

Type groupExamples
Boolean and numericbool, int, float
Unity structsVector2, Vector3, Quaternion
Textstring
Packed databyte[]
ArraysSupported primitive and struct arrays

Prefer byte[] for high-frequency values because it lets you control byte layout and avoid many small field messages.

Direction

[TransSync("state", Direction = NetworkSyncDirection.SendReceive)]
public int state;
DirectionMeaning
SendReceiveSender writes it and receiver applies it.
SendOnlySender writes it but this field is not used as a receiver target.
ReceiveOnlyReceiver applies it but this field is not written by the sender.

Conditional enable

public bool sendVelocity = true;

[TransSync("velocity", EnabledBy = nameof(sendVelocity))]
public Vector3 velocity;

EnabledBy points to a boolean field on the same component. When false, the variable is skipped. If a variable message would have no fields, it is removed from the payload.

Receive interpolation

Each TSMPNetworkBehaviour has receiveInterpolation:

ModeBehaviour
NoneIgnore received values.
DiscreteApply values when a frame is decoded.
ContinuousSmooth toward received values where the component implements continuous application.

Custom components can use GetReceiveInterpolationStep() to drive their own smoothing.

Setup requirement

After adding, removing, or renaming [TransSync] fields, run Apply Setup. TSMP uses generated binding tables instead of runtime reflection in uploaded worlds.