terminalCode Example
Sync Properties for Networked State
Sync Properties for Multiplayer
The [Sync] attribute automatically synchronizes property values to all clients when they change.
Basic Sync
CSHARP
public sealed class HealthComponent : Component
{
// Automatically synced to all clients
[Sync]
public float Health { get; set; } = 100f;
[Sync]
public bool IsDead { get; set; }
}Sync Flags
Customize sync behavior with SyncFlags:
CSHARP
// Host-authoritative - only host can change
[Sync(SyncFlags.FromHost)]
public float GameTime { get; set; }
// Interpolated - smooth value changes over ticks
[Sync(SyncFlags.Interpolate)]
public Vector3 Position { get; set; }
// Combined flags
[Sync(SyncFlags.FromHost | SyncFlags.Interpolate)]
public Vector3 PlayerPosition { get; set; }| Flag | Description |
|---|---|
| SyncFlags.Query | Enables Query Mode for late-joiners |
| SyncFlags.FromHost | Only host can modify the value |
| SyncFlags.Interpolate | Smoothly interpolate value changes |
Important Notes
- NetList<T> and NetDictionary<K,V> do not support [Property] attribute
- Sync only sends when values actually change
- Use FromHost for game state that should be server-authoritative
Health Component Example
CSHARP
public sealed class PlayerHealth : Component
{
[Sync]
public float CurrentHealth { get; private set; } = 100f;
[Sync(SyncFlags.FromHost)]
public float MaxHealth { get; set; } = 100f;
public void TakeDamage(float damage)
{
// If not host, request damage via RPC
if (!Networking.IsHost)
{
RequestDamageRpc(damage);
return;
}
CurrentHealth = MathF.Max(0, CurrentHealth - damage);
}
[Rpc.Broadcast]
private void RequestDamageRpc(float damage)
{
if (Networking.IsHost)
TakeDamage(damage);
}
}
Was this helpful?