Skip to main content

PositionSerializer

A class in Root.Township, the global namespace.

Writes and reads an entity's position and rotation for the network, optionally relative to a moving context such as a cart or boat it stands on. PositionSerializer<T> adds a typed FixParent.

Constructors​

MemberWhat it is
PositionSerializer(NetworkEntity entity)Creates a PositionSerializer from entity.

Fields​

MemberWhat it is
Vector3 positionLast world position written or received.
Quaternion rotationLast world rotation written or received.
protected bool isContextChangedSet on read when the context changed from the previous one.
protected Transform lastContextParentThe transform FixParent last parented to.
7 private and internal fields
MemberWhat it is
private IMovementContext contextBacking field for Context.
private NetworkEntity entityThe entity whose position this serializes.
private MemoizeOne<uint, IMovementContext> getContextCaches the last id to context lookup.
private static readonly Vector3 LocalMaximumUpper bound (100 on each axis) for the position sent relative to a movement context.
private static readonly Vector3 LocalMinimumLower bound (-100 on each axis) for the position sent relative to a movement context.
private Vector3 localPositionPosition relative to the movement context.
private Quaternion localRotationRotation relative to the movement context.

Properties​

MemberWhat it is
IMovementContext Context { get; set; }The moving object the entity is positioned relative to, or null. Cleared if that object is destroyed.
Vector3 LivePosition { get; }World position, worked out from the context's current transform when there is one.
Quaternion LiveRotation { get; }World rotation, worked out from the context's current transform when there is one.

Methods​

MemberWhat it is
void FixParent(Transform targetTransform)Parents the transform to the context's transform, or unparents it when the context goes away.
void ForceRemotePosition(Vector3 position)Sets the stored world position and updates the context-local copy to match.
void ForceRemoteRotation(Quaternion rotation)Sets the stored world rotation and updates the context-local copy to match.
void Serialize(Stream stream)Writes or reads position, rotation and the context id plus local offset. Readers that aren't authoritative pick up the new context.
2 private and internal methods
MemberWhat it is
private IMovementContext GetContext(uint id)Finds the movement context of the entity with an id. Returns null for 0 or a missing entity.
private void RemoveContext(NetworkEntity entity, bool isUnload)Clears the context when its entity is destroyed.

Used in​

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