SA-MP Forums Archive
[Tutorial] Handlers - Pawn.RakNet - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Handlers - Pawn.RakNet (/showthread.php?tid=647162)



Handlers - Pawn.RakNet - Jelly23 - 30.12.2017

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

With handlers, you can assign a callback to handle packets and RPCs. Basically, it would be the same as using them in the main callbacks (OnIncomingRPC/Packet and OnOutcomingRPC/Packet), except only the RPC/Packet you assigned to that specific callback will reach it.

Handler types:
-PR_INCOMING_RPC: RPCs sent by the client to the server.
-PR_INCOMING_PACKET: Packets sent by client to the server.
-PR_OUTCOMING_RPC: RPCs sent by the server to the cliente.
-PR_OUTCOMING_PACKET: Packets sent by the server to the client.
Function:
Code:
PR_RegHandler(id, const publicname[], PR_HandlerType:type)
id - The packet/RPC id you are going to create the handler for.
publicname - Callback used by the handler.
type - Handler type.
• Creating a handler
To create a handler is pretty simple. Take into account that the handler's callback must have two parameters, one for the player receiving/sending the packet/RPC (playerid) and another one for the BitStream (BitStream:bs).

PHP Code:
#define ID_PLAYER_SYNC  207
public OnGameModeInit()
{
    
PR_RegHandler(ID_PLAYER_SYNC"OnFootSync"PR_INCOMING_PACKET);
    return 
1;
}
forward OnFootSync(playeridBitStream:bs);
public 
OnFootSync(playeridBitStream:bs)
{
    new 
On_FootSync[PR_OnFootSync];
    
BS_IgnoreBits(bs8);
    
BS_ReadOnFootSync(bsOn_FootSync);
    
printf("My weapon id is: %i"On_FootSync[PR_weaponId]);
    return 
1;

The example above is pretty easy to understand, we can manipulate the packet 207 (on foot sync) through OnFootSync callback.

I'm not going to explain why you should ignore 8 bits and such, that is something I explained in my last Pawn.RakNet tutorial about packets. I recommend you to read my two last tutorials about RPCs and Packets before reading this thread.
• Errors related with handlers
These are two errors that might show up in your logs/console:
n_PR_RegHandler: Public "YourCallbackName" does not exist. - Pretty obvious, the callback you assigned to your handler does not exist.
n_PR_RegHandler: invalid array(T, N) subscript - Your RPC/Packet ID is undefined. For example, your RPC id is above/below MAX_RPC_MAP_SIZE (0-254) which is undefined/invalid.
• Considerations
- Take into account that OnIncomingPacket/RPC and OnOutcomingPacket/RPC callbacks will always be called before any handler's callback. So, if you return 0 in the main callbacks, handler callbacks won't be called.

-There can only be one handler per packet/RPC. If you create two handlers for the same packet/RPC, only the last one you created for that packet/RPC will work.

-If you create a handler and not assign any callback to it (leave the 2nd parameter empty/null), the server will crash.



Re: Handlers - Pawn.RakNet - RIDE2DAY - 01.01.2018

Awesome, as always. People should understand how useful Pawn.Raknet is.


Re: Handlers - Pawn.RakNet - rfr - 01.01.2018

very good


Re: Handlers - Pawn.RakNet - RogueDrifter - 01.01.2018

Seeing your releases makes me wish i knew more about pawn.raknet i'm still waiting to get a chance to read and learn more about it, very interesting gj.


Re: Handlers - Pawn.RakNet - kurta999 - 01.01.2018

Very nice!

Now this plugin is a good YSF alternative, so I can remove half of the YSF because I can't maintain it anymore. If you will have free time and you'll be bored, then you could write a tutorial how to use RPC functions from this plugin as alternatives to ysf functions? (like. SetPlayerGravity, SetPlayerAttachedObjectForPlayer, etc..)

Thanks.


Re: Handlers - Pawn.RakNet - Dayrion - 29.01.2018

Useful to note you can assign multiple id to one handler.
Simple exemple: OnActorStateChange is called when a actor is streamed in or out for a player.
PHP Code:
PR_RegHandler(0xAB"OnActorStateChange"PR_OUTCOMING_RPC); 
PR_RegHandler(0xAC"OnActorStateChange"PR_OUTCOMING_RPC); 



Re: Handlers - Pawn.RakNet - Jelly23 - 29.01.2018

Quote:
Originally Posted by Dayrion
View Post
Useful to note you can assign multiple id to one handler.
Simple exemple: OnActorStateChange is called when a actor is streamed in or out for a player.
PHP Code:
PR_RegHandler(0xAB"OnActorStateChange"PR_OUTCOMING_RPC); 
PR_RegHandler(0xAC"OnActorStateChange"PR_OUTCOMING_RPC); 
Yes, you can have a single callback for different handlers, but I wouldn't recommend it.

A handler callback has only two parameters: one for the player id and another one for the BitStream. There is no way you could identify which RPC/Packet called that callback, unless you compare the length in bits of the stream, by using BS_GetNumberOfBitsUsed.