SA-MP Forums [Tutorial] Write & Read offsets - Pawn.RakNet
 New Account Members List Search Today's Posts Mark Forums Read

10/04/2018, 12:22 AM   #1
BrunoBM23
Big Clucker

Join Date: Mar 2018
Posts: 49
Reputation: 72
Write & Read offsets - Pawn.RakNet

Write & Read offsets - Pawn.RakNet

I have seen people in doubt when it comes about write and read offsets in a bitstream, which is why I made this tutorial.

Before we can understand how they work and what the offsets of a bitstream parameter would be, we have to know how many bits each value type is equivalent to when inside the stream. Below, we have a table with such information:

 Type Bits Bytes FLOAT 32 4 (U)INT32 32 4 (U)INT16 16 2 (U)INT8 8 1 BOOL 1 0,125

Assuming you have understood what each type is equivalent to, let's take the information below into account:

To read/write into the offset of the parameter of your choice, you are gonna need the sum of the amount of bits of the parameters previous to the one we intend on reading/writing.

Let's carry on with an example:

PHP Code:
``` new BitStream:my_bs = BS_New(); BS_WriteValue (     my_bs,     PR_UINT16, 1,     PR_UINT32, 2,     PR_UINT16, 3,     PR_UINT8, 4 );  ```
On the bitstream above, we have a total of 9 bytes, which are equivalent to 72 bits, because 8 bits = 1 byte.

Let's suppose we intend to read the value of the last parameter, which is 4 and the type is UINT8. There are two ways we can use to read this value, which would be: by using BS_SetReadOffset to set the read offset, or BS_IgnoreBits to ignore the other parameters' bits so we can read only the last 8.

So, knowing that with the sum of the previous parameters' bits we can reach the parameter we want's offsets, let's take into account the examples below:

Example 1 - BS_IgnoreBits
With the sum of the bits of the parameters previous to the last, we are gonna have 64 bits (16 + 32 + 16). We are going to ignore those 64 bits, using the function this way:

PHP Code:
``` BS_IgnoreBits(my_bs, 64);  ```
After ignoring these 64 bits we don't intend on reading, we can finally read the last parameter:

PHP Code:
``` new var;      BS_ReadValue (     my_bs,     PR_UINT8, var );  ```
Then, only the last 8 bits are going to be read.

The logic is pretty much the same here, we are going to sum the bits of the parameters previous to the last one, then, we are going to change the read offsets to that sum (64).

PHP Code:
``` BS_SetReadOffset(my_bs, 64);  ```
After changing the read offsets to 64, we can read the last parameter:

PHP Code:
``` new var;      BS_ReadValue (     my_bs,     PR_UINT8, var );  ```

Just like in the first example, only the last 8 bits are going to be read.

Take into account that the same logic applies to write offsets. Just like we have the functions BS_SetReadOffset, BS_GetReadOffset and BS_ResetReadPointer for read offsets, we have BS_SetWriteOffset, BS_GetWriteOffset and BS_ResetWritePointer for write offsets. This is what these functions are for:

BS_SetWriteOffset - Sets the write offsets.
BS_GetWriteOffset - Gets the current write offset.
BS_ResetWritePointer - This function will reset the write offsets (0).

Some people think you are limited to writing/reading data only once by using BS_WriteValue/BS_ReadValue a single time, but that is not true. As explained previously, a bitstream has read & write offsets, and those are used to identify where you are reading/writing data on, and because of such this is possible:

PHP Code:
``` new BitStream:my_bs = BS_New(); BS_WriteValue (     my_bs,     PR_UINT16, 1 ); BS_WriteValue (     my_bs,     PR_UINT32, 2 ); BS_WriteValue (     my_bs,     PR_UINT16, 3 ); BS_WriteValue (     my_bs,     PR_UINT8, 4 );  ```
On the example above, we wrote 72 bits in our bitstream. That way would be equivalent to:

PHP Code:
``` new BitStream:my_bs = BS_New(); BS_WriteValue (     my_bs,     PR_UINT16, 1,     PR_UINT32, 2,     PR_UINT16, 3,     PR_UINT8, 4 );  ```

Some RPCs have pretty long structures, so being able to use BS_WriteValue/BS_ReadValue more than one time is benefitial, since functions are limited to 32 parameters in Pawn.

Last edited by BrunoBM23; 17/08/2018 at 04:35 PM.

10/04/2018, 12:25 AM   #2
RogueDrifter
High-roller

Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,144
Reputation: 345
Re: Write & Read offsets - Pawn.RakNet

Great, thanks for sharing this! (why does this feel like a deja vu )
__________________
Quote:
 I'm not using the forum to release stuff anymore, if you want updates for my releases or to check my new releases check the links below >
Quote:
 Originally Posted by Escobar ⚠ Life sucked me in and shat me on the cruel world.

 24/06/2018, 12:07 AM #3 J0sh... High-roller     Join Date: Aug 2014 Location: Hamburger Posts: 1,292 Reputation: 401 Re: Write & Read offsets - Pawn.RakNet yes now lets do more includes that call one rpc for one player mashallah EDIT: Got tricked... mashallah. Didn't notice the date, blasphemy, I blame you bruno! __________________ 🍔🍔🍔🍔🍔🍔🍔🍔🍔🍔🍔🍔 Yes I know my name has 3 dots. Thanks for noticing, it was meant to signify a pause in the personal message asking to be named J0sh but they got included.
 28/06/2018, 09:33 AM #4 Sasino97 High-roller     Join Date: Dec 2010 Location: Italy Posts: 1,440 Reputation: 106 Re: Write & Read offsets - Pawn.RakNet Thank you for sharing. __________________ Desktop, Web, Mobile and Game DeveloperSA-MP Game Modes and Filter Scripts (PAWN, SQL) (8 years experience) Windows Applications (WPF, UWP) Android Applications (Java Android SDK, Xamarin) Web Design and Development (Wordpress, ASP.NET MVC, PHP, SQL) Game Development (Unity3D, GameMaker Studio, LibGDX) sasinosoft@gmail.com I hope I don't get banned for using gmail, lol

 Thread Tools Display Modes Linear Mode

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post [Plugin] Pawn.RakNet YourShadow Plugin Development 202 03/08/2018 07:29 PM [Tutorial] Offsets de escrita/leitura - Pawn.RakNet BrunoBM23 Lançamentos/Releases 5 18/07/2018 04:26 PM [Tutorial] RPC - Pawn.RakNet Jelly23 Tutorials 21 13/12/2017 09:51 PM Ahmad45123 Tutorials 8 13/03/2015 02:41 AM

All times are GMT. The time now is 01:54 AM.

 -- (EN) English -- (RU) Русски Contact Us - SA-MP - Archive - Top