D
D
DOTSNET
Search…
NetworkComponent
DOTSNET can automatically sync components for you.
For example, let's say we need a Health component with:
    Current health: this changes when attacked / when recovering.
    Max health: this never changes.
We can define a Health : NetworkComponent , which DOTSNET will automatically:
    Include in Spawn messages
    Synchronize every NetworkServer.snapshotInterval seconds

NetworkComponent Interface

First, we need to create a Health struct that implements the NetworkComponent interface:
1
public struct Health : NetworkComponent
2
{
3
public int current;
4
public int max;
5
6
// server authoritative
7
public SyncDirection GetSyncDirection() =>
8
SyncDirection.SERVER_TO_CLIENT;
9
10
// only sync current. max is always the same.
11
public bool Serialize(ref BitWriter128 writer) =>
12
writer.WriteInt(current);
13
14
// only sync current. max is always the same.
15
public bool Deserialize(ref BitReader128 reader) =>
16
reader.ReadInt(out current);
17
}
18
19
// need to define a serializer for every NetworkComponent
20
public class HealthSerializer : NetworkComponentSerializer<Health> {}
Copied!
Important: we also need to define a NetworkComponentSerializer<T> for every NetworkComponent .
NetworkComponentimplements IComponentData automatically and is burstable.
In the above example, DOTSNET will automatically serialize current health and include it in Snapshots, from client to server. If the client tries to modify it, it will have no effect unless we change sync direction to CLIENT_TO_SERVER which would make it client authoritative.
We could avoid the Serializer defintion with C# Reflection, but it's too slow.

NetworkComponentSerializer<T> Fast

The one-liner NetworkComponentSerializer<T> uses an EntityQuery<T> internally because a generic Entites.ForEach<T> is not supported by ECS.
If necessary, feel free to overwrite the Serializer's SerializeAll / DeserializeAll functions for maximum performance. It's more verbose though:
1
public class HealthSerializer : NetworkComponentSerializer<Health>
2
{
3
public override void SerializeAll(CurrentWorld currentWorld)
4
{
5
ushort key = Key; // copy for Burst
6
Entities.ForEach((ref NetworkComponentsSerialization serialization, in NetworkIdentity identity, in Health component) =>
7
{
8
Serialize(currentWorld, key, identity, component, ref serialization);
9
})
10
.Run();
11
}
12
13
public override void DeserializeAll(CurrentWorld currentWorld)
14
{
15
ushort key = Key; // copy for Burst
16
Entities.ForEach((ref NetworkComponentsDeserialization deserialization, ref Health component, in NetworkIdentity identity) =>
17
{
18
Deserialize(currentWorld, key, identity, ref component, ref deserialization);
19
})
20
.Run();
21
}
22
}
Copied!
For your own components, simply copy the above code and replace Health with your components type. Put that code right below where you define your component, in the same file. Don't worry about what it does. it just needs to be defined.
Defining NetworkComponentSerializer<T> might be automated in the future.
Last modified 5mo ago