Skip to main content

EntitySerializer

A class in Root.Township, Alta.Networking.

Writes and reads an entity's streamed state: the combined bits of all its INetworkSerialized behaviours. The side that owns the state writes it when something changed and resends it a limited number of times. Get it from NetworkEntity.Serializer.

Constructors​

MemberWhat it is
EntitySerializer(NetworkEntity entity, INetworkSerialized[] behaviours)Creates an EntitySerializer from entity and behaviours.

Fields​

MemberWhat it is
Player AuthorityPlayerThe player who owns the state (their own body), or null.
bool IsAuthoritativeTrue if this side writes the state.
static AccumulativeAverageNumber PrepareDurationShared timing average for the prepare step.
static AccumulativeAverageNumber SerializationDurationShared timing average for the write step.
12 private and internal fields
MemberWhat it is
private INetworkSerialized[] behavioursThe entity's INetworkSerialized behaviours, serialized in this order.
private Chunker chunkerThe entity's chunker, taken from it as an IChunkedEntity.
private EntityStreamerData dataBuffer for regular (delta) updates, with its bit count and unchanged count.
private NetworkEntity entityThe network entity this serializer writes.
private EntityStreamerData fullDataBuffer for full updates sent to players who need the whole state.
private bool isEnabledWhether the entity was enabled at the last serialize. Disabled entities don't send.
private static Logger loggerNLog logger. Logs when a behaviour's MaximumBits is too small for what it wrote.
private int resendCountSends since the data last changed. Reset by MarkDataAsChanged.
private static StreamWriter sharedWriterOne stream writer shared by every serializer to fill buffers.
private static Stopwatch timerShared stopwatch used to time prepare and serialize for the profiling averages.
private const int UnchangedHalfCheckCountAfter 10 unchanged writes, off-frame serialization is skipped.
private const int UnchangedResendAfter 30 sends with no change, IsSending turns false and the entity stops being resent.

Properties​

MemberWhat it is
uint DataBitCount { get; }Size in bits of the last written delta data.
uint Identifier { get; private set; }The entity's identifier.
bool IsSending { get; }True while the entity is enabled and the data has been sent fewer than 30 times since it last changed.

Events​

MemberWhat it is
static event SerializedEntityHandler ReadEntityRaised for every entity state read. Called very often.
static event SerializedEntityHandler WroteEntityRaised for every entity state written. Called very often.

Methods​

MemberWhat it is
bool IsEntitySpawnedFor(IPlayer player)True if the entity has been spawned for the given player.
void MarkDataAsChanged()Forces the state to be written and sent again.
void ReadDataFromStream(StreamReader reader, uint messageSize)Reads delta state into the behaviours from a network stream.
void ReadFullDataFromStream(StreamReader reader, uint messageSize)Reads full state into the behaviours from a network stream.
void ReleaseArrays()Returns the data buffers to the pool. The serializer can't be used after.
void SerializeData(bool isOffFrame)Writes the delta state into the internal buffer if a behaviour reports a change.
void SerializeFullData()Writes the full state (for newly spawned players) into the internal buffer.
void WriteDataIntoStream(StreamWriter writer)Copies the delta buffer into an outgoing stream.
void WriteFullDataIntoStream(StreamWriter writer)Copies the full buffer into an outgoing stream.
4 private and internal methods
MemberWhat it is
private void ReadFromStreamInternal(StreamReader reader, bool isFull, uint messageSize)Reads every behaviour's data from the stream, marks data changed if not authoritative, and fires ReadEntity.
private void SerializeDataInternal(bool isFull, EntityStreamerData data, bool isOffFrame)Asks behaviours whether they changed, writes them into the buffer if so, and fires WroteEntity. Skips off-frame writes once unchanged 10 times.
private void SetupDataArrays()Sizes the regular and full data buffers from the behaviours' MaximumBits and takes them from the array pool.
private void WriteIntoStreamInternal(StreamWriter writer, EntityStreamerData data)Copies the buffered bits into the outgoing stream.

Used in​

Members of other types that take, return or hold a EntitySerializer: