SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Tutorials

Thread Tools Display Modes
Old 15/11/2017, 10:21 PM   #1
Big Clucker
Jelly23's Avatar
Join Date: Aug 2017
Posts: 89
Reputation: 159
Default Packets - Pawn.RakNet

• Packets - Pawn.RakNet
Other Pawn.RakNet tutorials: RPC - Pawn.RakNet

Systems that run RakNet, communicate through what is known as packets. Or more accurately in the case of UDP, datagrams. Each datagram is created by RakNet and contains one or more messages. Those messages/data could be position, armor, actual vehicle and so on.

The first byte of the packet (8 bits) is it's numeric identifier.

There are different packet identifiers, the most common being:
  • ID_VEHICLE_SYNC (200) = Responsible for vehicle synchronization data. Example: when the player is inside a car. Their position, car health, speed, etc, is the packet data.
  • ID_AIM_SYNC (203) = Responsible for camera synchronization data. Example: Camera vectors, camera zoom, weapon status, object/car/player/actor the player is targeting, etc, is the packet data.
  • ID_BULLET_SYNC (206) = Responsible for bullet synchronization data. Ex: hit type, hit origin, hit target, center of hit, weapon the bullet belongs to, etc, is the packet data.
  • ID_PLAYER_SYNC (207) = Responsible for on foot synchronization data. Ex: keys pressed, current position, health, armor, current weapon, speed, animations, surfing offsets, if the player is surfing some vehicle/object, special action, etc, is packet data.
• What is BitStream?
The BitStream class is a helper class in RakNet library that is used to wrap a dynamic array for the purpose of packing and unpacking bits.
• Modifying packet data
PHP Code:
#define ID_PLAYER_SYNC  207

public OnIncomingPacket(playeridpacketidBitStream:bs)
    if (
packetid == ID_PLAYER_SYNC)



onFootData[PR_weaponId] = 38;



In the example above, we modify the player's current weapon in the packet, thus making everyone see them holding a minigun (38).

BS_IgnoreBits(bs, 8); - Before reading the packet data, we ignore the first 8 bits (1 byte) because they contain the packet identifier.

BS_ReadOnFootSync(bs, onFootData); - After ignoring the first 8 bits, we can read the packet, which will have its data passed to onFootData (referencing the enumerator PR_OnFootSync).

onFootData[PR_weaponId] = 38; - We change the weapon id in the packet to anything we want. The id we set will be the one sent in the packet, and it will be the weapon other players will see.

BS_SetWriteOffset(bs, 8); - We set the write offset after the 8 bits, because we don't want to write over the packet identifier.

BS_WriteOnFootSync(bs, onFootData); - Then, we write the new on foot synchronization data in the BitStream.

return 1; - We return 1, allowing the packet to be sent with the new data we wrote in the BitStream.

• Sending a packet
To send a packet, we have to write it's data in a BitStream, the first parameter being it's identifier.

PHP Code:
#define ID_BULLET_SYNC  206

BitStream:bs BS_New();





/*Somewhere in your code*/
new bulletData[PR_BulletSync];

bulletData[PR_hitType] = /*Your hit type*/;
bulletData[PR_hitId] = /*hit id*/;

bulletData[PR_origin][0] = /*origin X*/;
bulletData[PR_origin][1] = /*origin Y*/;
bulletData[PR_origin][2] = /*origin Z*/;
bulletData[PR_hitPos][0] = /*hit position X*/;
bulletData[PR_hitPos][1] = /*hit position Y*/;
bulletData[PR_hitPos][2] = /*hit position Z*/;
bulletData[PR_offsets][0] = /*offset X*/;
bulletData[PR_offsets][1] = /*offset Y*/;
bulletData[PR_offsets][2] = /*offset Z*/;
bulletData[PR_weaponId] = /*weapon id*/;
In the example above, the function sends a packet with bullet data to a specific player.

new BitStream:bs = BS_New(); - We create a new BitStream, where we are going to write our packet data.

PR_UINT8, ID_BULLET_SYNC, - First parameter of the BitStream. That is the packet identifier.

PR_UINT16, from - Second parameter of the BitStream. This is the responsible player, from whom the bullet data will originate.

BS_WriteBulletSync(bs, data); - "data" references the enumerator PR_BulletSync and contains the bullet data we send in the function's 3rd parameter, it's data will be written in the BitStream.

BS_Send(bs, to); - "to" is the player about to receive the packet. If "to" is -1, all players will receive the packet.

BS_Delete(bs); - After we are done, we remove the BitStream.

Last edited by Jelly23; 24/11/2017 at 04:34 PM.
Jelly23 is offline   Reply With Quote
Old 16/11/2017, 06:45 PM   #2
Xeon™'s Avatar
Join Date: Jul 2017
Location: Earth
Posts: 516
Reputation: 73
Default Re: Packets - Pawn.RakNet

Good Job!

Well explained!
Xeon™ is online now   Reply With Quote
Old 16/11/2017, 06:46 PM   #3
Battlezone's Avatar
Join Date: Aug 2013
Posts: 832
Reputation: 134
Default Re: Packets - Pawn.RakNet

This needs more appreciation and attention from the community, definitely one of those useful tutorials.
Battlezone is offline   Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] RPC - Pawn.RakNet Jelly23 Tutorials 20 09/12/2017 07:20 AM
[Plugin] Pawn.RakNet - write the best anticheat YourShadow Plugin Development 144 08/12/2017 07:08 PM
[Tutorial] Pacotes - Pawn.RakNet Jelly23 Lançamentos/Releases 29 22/11/2017 10:27 PM
Packets, and timeout biker122 Scripting Help 7 04/07/2016 06:28 PM
Packets lose... MaTrIx4057 Everything and Nothing 15 30/08/2009 05:10 PM

All times are GMT. The time now is 08:30 PM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.