• 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.Function:
-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.
id - The packet/RPC id you are going to create the handler for.Code:PR_RegHandler(id, const publicname[], PR_HandlerType:type)
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).
The example above is pretty easy to understand, we can manipulate the packet 207 (on foot sync) through OnFootSync callback.PHP Code:
#define ID_PLAYER_SYNC 207
public OnGameModeInit()
{
PR_RegHandler(ID_PLAYER_SYNC, "OnFootSync", PR_INCOMING_PACKET);
return 1;
}
forward OnFootSync(playerid, BitStream:bs);
public OnFootSync(playerid, BitStream:bs)
{
new On_FootSync[PR_OnFootSync];
BS_IgnoreBits(bs, 8);
BS_ReadOnFootSync(bs, On_FootSync);
printf("My weapon id is: %i", On_FootSync[PR_weaponId]);
return 1;
}
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.
PR_RegHandler(0xAB, "OnActorStateChange", PR_OUTCOMING_RPC);
PR_RegHandler(0xAC, "OnActorStateChange", PR_OUTCOMING_RPC);
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:
|