menu_bookDocumentation
Sync Properties: Automatic Property Replication with [Sync] Attribute
Adding the [Sync] attribute to a property on a Component will have its latest value sent to other players each time it changes. Only the owner of the object can change sync properties.
CSHARP
public class MyComponent : Component
{
[Sync] public int Kills { get; set; }
}Supported Types
[Sync] supports unmanaged types and string. Any value type including structs works: int, bool, Vector3, float. Also supports serializing GameObject, Component, and GameResource.Detecting Changes
Apply [Change] attribute alongside [Sync] to get a callback when the value changes:
CSHARP
public class MyComponent : Component
{
[Sync, Change( "OnIsRunningChanged" )] public bool IsRunning { get; set; }
private void OnIsRunningChanged( bool oldValue, bool newValue )
{
// The value of IsRunning has changed
}
}SyncFlags
| Flag | Description |
|---|---|
| SyncFlags.Query | Value is checked for changes every network update instead of on set |
| SyncFlags.FromHost | Host has ownership over the value instead of the object owner |
| SyncFlags.Interpolate | Value is interpolated for other clients over a few ticks |
Collections
Use NetList<T> and NetDictionary<K,V> for networked collections:
CSHARP
public class MyComponent : Component
{
[Sync] public NetList<int> List { get; set; } = new();
[Sync] public NetDictionary<AmmoCount, int> Dictionary { get; set; } = new();
}Query Mode
Use SyncFlags.Query when the backing field can be modified outside the property setter (e.g., by a separate method). The value is checked for changes every network update rather than relying on setter detection:
CSHARP
Vector3 _velocity;
[Sync( SyncFlags.Query )]
public Vector3 Velocity
{
get => _velocity;
set => _velocity = value;
}
Was this helpful?