-
Notifications
You must be signed in to change notification settings - Fork 12
Packets
Valk edited this page Feb 5, 2026
·
25 revisions
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 partial 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; }
}The following is what is outputted by the source gen for you:
// You do not need to write this code, it is generated for you!
public partial class CPacketPlayerPosition
{
public override void Write(PacketWriter writer)
{
writer.Write(Id);
writer.Write(Position);
}
public override void Read(PacketReader reader)
{
Id = reader.ReadUInt();
Position = reader.ReadVector2();
}
}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 partial 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();
}
}
}By default 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, you have 2 options.
- (Recommended) Add a
OpcodeEnum Opcode { get; set; }property to any of your packet classes with a conditional if-else chain kind of like what you see above but instead of a bool it is a enum. ReplaceOpcodeEnumwith your own enum. (All enums are converted to bytes so you cannot have more than256options in any enum you send over the network! This may be changed in the future.) - In
PacketRegistry.cs, changebytetoushortbut note that now all your packets will have a 2 byte overhead instead of just 1 byte. Note thatushorthas a size of65,535compared tobytethat only has255.
// res://Framework/Netcode/Packet/PacketRegistry.cs
[PacketRegistry(typeof(byte))]
public partial class PacketRegistry
{
}