OnPlayerUpdate
From SA-MP Wiki
Description:
Note | This callback can also be called by NPC. |
playerid | ID of the player sending an update packet. |
Return Values:
- 0 - Update from this player will not be replicated to other clients.
- 1 - Indicates that this update can be processed normally and sent to other players.
- It is always called first in filterscripts.
Example: Make your own callback - OnPlayerChangeWeapon(playerid, oldweapon, newweapon)
public OnPlayerUpdate(playerid) { new iCurWeap = GetPlayerWeapon(playerid); // Return the player's current weapon if(iCurWeap != GetPVarInt(playerid, "iCurrentWeapon")) // If he changed weapons since the last update { // Lets call a callback named OnPlayerChangeWeapon OnPlayerChangeWeapon(playerid, GetPVarInt(playerid, "iCurrentWeapon"), iCurWeap); SetPVarInt(playerid, "iCurrentWeapon", iCurWeap);//Update the weapon variable } return 1; // Send this update to other players. } stock OnPlayerChangeWeapon(playerid, oldweapon, newweapon) { new s[128], oWeapon[24], nWeapon[24]; GetWeaponName(oldweapon, oWeapon, sizeof(oWeapon)); GetWeaponName(newweapon, nWeapon, sizeof(nWeapon)); format(s, sizeof(s), "You changed weapon from %s to %s!", oWeapon, nWeapon); SendClientMessage(playerid, 0xFFFFFFFF, s); }
The example above shows you how you easily can create a callback, example shows OnPlayerChangeWeapon, with playerid and old/new weapon parameters, you can make TONS of others too! (Note, in the example you'd probably like to reset iCurrentWeapon variable upon player connect (OnPlayerConnect) and remember to forward OnPlayerChangeWeapon!)
Example: Track players health changing!
public OnPlayerUpdate(playerid) { new Float:fHealth; GetPlayerHealth(playerid, fHealth); if(fHealth != GetPVarFloat(playerid, "faPlayerHealth")) { // Player health has changed since the last update -> server, so obviously thats the thing updated. // Lets do further checks see if he's lost or gained health, anti-health cheat? ;) if(fHealth > GetPVarFloat(playerid, "faPlayerHealth")) { /* He has gained health! Cheating? Write your own scripts here to figure how a player gained health! */ } else { /* He has lost health! */ } SetPVarFloat(playerid, "faPlayerHealth", fHealth); } }
Here's another neat script, possibly good for anti health hacks? Possibly VERY good? If you use another function for SetPlayerHealth so you can store the health you've set to players, you'll be able to track the health status at hundred percent accuracy, so you'll have a great chance to prevent health hacks totally - as it's quite limited where people can gain health through the SP possibilities - just vending machines and shops basically.