SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 30/12/2017, 07:26 PM   #1
Jelly23
Guest
 
Posts: n/a
Default Handlers - Pawn.RakNet

• 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.

Last edited by Jelly23; 29/01/2018 at 11:52 PM.
  Reply With Quote
Old 01/01/2018, 12:30 PM   #2
RIDE2DAY
Huge Clucker
 
RIDE2DAY's Avatar
 
Join Date: Jul 2015
Location: Modena, Italy
Posts: 219
Reputation: 375
Default Re: Handlers - Pawn.RakNet

Awesome, as always. People should understand how useful Pawn.Raknet is.
RIDE2DAY is offline   Reply With Quote
Old 01/01/2018, 07:07 PM   #3
rfr
Banned
 
Join Date: Nov 2017
Posts: 278
Reputation: 43
Default Re: Handlers - Pawn.RakNet

very good
rfr is offline   Reply With Quote
Old 01/01/2018, 07:21 PM   #4
RogueDrifter
High-roller
 
RogueDrifter's Avatar
 
Join Date: Dec 2017
Location: SA-MP Drifting world.
Posts: 1,144
Reputation: 344
Default Re: Handlers - Pawn.RakNet

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.
__________________
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.

[Github]:Link [Gists]:Link [Forum]:Link [Server]:Link [Discord]:Link


RogueDrifter is offline   Reply With Quote
Old 01/01/2018, 07:28 PM   #5
kurta999
High-roller
 
kurta999's Avatar
 
Join Date: Apr 2009
Location: Slovakia (HU)
Posts: 1,420
Reputation: 626
Default Re: Handlers - Pawn.RakNet

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.
__________________
I don't play and have ANY interest in SA:MP anymore! Please do not ask for help, because I'm not going to reply.
kurta999 is online now   Reply With Quote
Old 29/01/2018, 11:09 PM   #6
Dayrion
High-roller
 
Dayrion's Avatar
 
Join Date: Jan 2016
Location: France
Posts: 1,916
Reputation: 256
Default Re: Handlers - Pawn.RakNet

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); 
__________________
actor_plus - v5.0.2
« I'm a goner, somebody catch my breath,
I'm a goner, somebody catch my breath »
Dayrion is offline   Reply With Quote
Old 29/01/2018, 11:46 PM   #7
Jelly23
Guest
 
Posts: n/a
Default Re: Handlers - Pawn.RakNet

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.
  Reply With Quote
Reply

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
[Plugin] Pawn.RakNet YourShadow Plugin Development 202 03/08/2018 07:29 PM
[Tutorial] Pacotes - Pawn.RakNet Jelly23 Lançamentos/Releases 33 23/06/2018 09:49 PM
[Tutorial] Handlers - Pawn.RakNet Jelly23 Lançamentos/Releases 14 02/01/2018 11:04 PM
[Tutorial] RPC - Pawn.RakNet Jelly23 Tutorials 21 13/12/2017 09:51 PM
[Tutorial] Packets - Pawn.RakNet Jelly23 Tutorials 2 16/11/2017 05:46 PM


All times are GMT. The time now is 08:31 AM.


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