s&box

Replicable Types

What is a Replicable Type

A replicable type is a type that can be described over the network. These are usually members of Entities and describe something about them that both the client and server need to be in sync with.

[Replicate] public float HeadSize{ get; set; }

Here we see a replicated member called HeadSize. In this instance float is the Replicable Type.

Defining Struct Replication Manually

To make a type replicable you need to let us know how to serialize it.

For a struct you can add these member functions.

public void Write( BinaryWriter writer ) { writer.Write( (float)x ); writer.Write( (float)y ); writer.Write( (float)z ); } public Vector3 Read( BinaryReader reader ) { return new Vector3( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle() ); }

Defining Class Replication Manually

For classes we have to be crafty because the class can be null we can't use members.

We can use extensions though - which we can use to handle null property if that's desired.

public static class MyClassExtension { public static void Write( this MyClass self, System.IO.BinaryWriter writer ) { if ( self == null ) { writer.Write( false ); return; } writer.Write( true ); writer.Write( self.Age ); writer.Write( self.Sex ); writer.Write( self.Location ); } public static MyClass Read( this MyClass self, System.IO.BinaryReader reader ) { int notNull = reader.ReadBoolean(); if ( notNull == 0 ) return null; if ( self == null ) self = new MyClass(); self.Age = self.Age.Read( reader ); self.Sex = self.Sex.Read( reader ); self.Location = self.Location.Read( reader ); return self; } }

This method also allows you to add replication to types that you're unable to edit for some reason.

NetworkClass

If your class derives from NetworkClass then you don't need to do this.

It will automatically become serializable and you can use [Replicate] attributes to serialize its members.

Page Links


Special Pages


Wikis

?

Render Time: 22ms

DB GetPage 4
Generate Html 3
SaveChanges (1) 12
Render Body 0
Render Sidebar 0