Skip to content

Custom Packets

Ian edited this page Jul 23, 2019 · 22 revisions

Creating your own packet type is easy, but to make it even easier i would simply copy and paste one of the pre-existing packets, and modify it to your own.

First, here are some simple rules about creating a packet:

  • A packet must inherit Packet
  • A packet must have a constructor, taking in a string and passing it to base.

Here is the definition for the TestPacket:

public class TestPacket : Packet<TestPacket>
{
        public string testString;

        public TestPacket(string packet_name) : base(packet_name){}

        public override TestPacket OpenPacketFromMessage(NetIncomingMessage msg)
        {
            TestPacket packet = new TestPacket(packet_identifier)
            {
                testString = msg.ReadString()
            };

            return packet;
        }

        public override NetOutgoingMessage PackPacketIntoMessage(NetOutgoingMessage msg,  TestPacket packet)
        {
            msg.Write(packet.testString);
            return msg;
        }
       
}
Clone this wiki locally