Skip to content

Handling Conditional Net Logic

Valk edited this page Oct 4, 2024 · 1 revision

🔍 Handling Conditional Logic

// Since we need to use if conditions we actually have to type out the Write and Read functions
public class SPacketPlayerJoinLeave : ServerPacket
{
    // Do not add [NetSend] attributes here as we are manually writing out the Write and Read functions
    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)
    {
        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();
        }
    }
}
Clone this wiki locally