-
Notifications
You must be signed in to change notification settings - Fork 12
Packets
Packets only contain the data to be sent and need to extend from ClientPacket or ServerPacket. All packets are prefixed with C for client or S for server, this is not required.
public class CPacketPlayerPosition : ClientPacket
{
public uint Id { get; set; }
public Vector2 Position { get; set; }
// Optionally exclude properties so they do not get sent
[NetExclude]
public Vector2 PrevPosition { get; set; }
}If a type is not supported, you will need to manually override Write and Read. You should also get a warning in the IDE telling you a type is not supported. The warning will go away when you override Write or Read.
| Type | Supported | Example Types | Additional Notes |
|---|---|---|---|
| Primitives | ✅ |
int, bool, ulong
|
|
| Vectors & byte[] | 🟧 |
Vector2, Vector3, byte[]
|
Only the supplied example types are currently supported. |
| Generics | 🟧 |
List<List<int>> Dictionary<string, List<char>>
|
Only the supplied example types are currently supported. |
| Arrays | ❌ | int[] |
|
| Classes & Structs | ❌ | PlayerData |
You have full control over the order of which data gets sent when you override Write and Read.
// Since we need to use if conditions we actually have to type out the Write and Read functions
public class SPacketPlayerJoinLeave : ServerPacket
{
public uint Id { get; set; }
public string Username { get; set; }
public Vector2 Position { get; set; }
public bool Joined { get; set; }
public override void Write(PacketWriter writer)
{
// Not required to cast explicit types but helps prevent human error
writer.Write((uint)Id);
writer.Write((bool)Joined);
if (Joined)
{
writer.Write((string)Username);
writer.Write((Vector2)Position);
}
}
public override void Read(PacketReader reader)
{
Id = reader.ReadUInt();
Joined = reader.ReadBool();
if (Joined)
{
Username = reader.ReadString();
Position = reader.ReadVector2();
}
}
}You can only have up to 256 different client packet classes and 256 different server packets for a total of 512 different packet classes.
If you need more space than this, add a OpcodeEnum Opcode { get; set; } property to any of your packet classes with a conditional if-else chain. Replace OpcodeEnum with your own enum.
All enums are converted to bytes so you cannot have more than 256 options in any enum you send over the network!