[size=22pt]Useful Function Thread[/size]
[size=12pt]FAQ[/size]
How do I get the compiler set?
Well, I made a re-upload of the compiler, comes with the IDE, includes and new.pwn
Works on compiling and is great to use click here to obtain it.
[size=18pt]ReturnPlayerName[/size]
Adding a player name to a formatted sentence
Code :#if !defined MAX_PLAYER_NAME #define MAX_PLAYER_NAME 24 // 24 Characters, I don't know the limit yet... #endif stock ReturnPlayerName(clientid) { new name[MAX_PLAYER_NAME]; GetPlayerName(clientid, name); return name; }
[size=14pt]Usage:[/size]
Code :printf("%s(%d) is cool", ReturnPlayerName(clientid), clientid);
[size=18pt]Efficent Variable creator[/size]
Lowers the bytes of a single array created
Code :#define MAX_PLAYERS 24 // Server slots #define Variable:%1<%2> %1[%2 char] new Variable: Hey < MAX_PLAYERS >;
Full explanation by Slice, Couldn't explain it very well.. I guess lol
[size=14pt]Explanation:[/size]
[size=18pt]GetPlayerPosFixed[/size][indent]PAWN has a feature for accessing single bytes in arrays, intended for use with packed strings. Most SA-MP natives doesn't cover packed strings, though.
You can, however, utilize these arrays a lot for memory optimization. A normal array can store values between -2,147,483,648 and 2,147,483,647; you don't always need that capacity, do you?
With packed strings you can store values between 0-255 (yes, no negative values; -1 will wrap to 255). When you know you don't need negative values and won't ever exceed 255, why not save some memory?
[pawn]new bool:g_IsPlayerSomething[ MAX_PLAYERS ]; // 500 cells * 4 bytes per cell = 2000 bytes!
new bool:g_IsPlayerSomething[ MAX_PLAYERS char ]; // 500 bytes = .. 500 bytes![/pawn]
If you use "char arrays" instead of normal ones where needed 50 times you'll save 75,000 bytes (~73 kb).
Note!
When accessing these arrays, you need to use curly brackets (aka braces) as opposed to the normal square brackets.
Example:Code :public OnPlayerConnect( clientID) { g_IsPlayerSomething{ clientID} = false; // ^ ^ }
Gathers a player position the more simple way.
Code :stock GetPlayerPosFixed(playerid, &Float:X, &Float:Y, &Float:Z) { X = GetPlayerPos(playerid, X_AXIS); Y = GetPlayerPos(playerid, Y_AXIS); Z = GetPlayerPos(playerid, Z_AXIS); }
[size=14pt]Usage:[/size]
Code :new Float: X, Float: Y, Float: Z; GetPlayerPosFixed(clientID, X, Y, Z); printf("Client %d is at the location X: %0.3f Y: %0.3f Z: %0.3f", clientID, X, Y, Z);
[size=18pt]IsPlayerInRangeOfPoint[/size]
If a player is near a point from a radius set.
Code :stock IsPlayerInRangeOfPoint(playerid, Float:radi, Float:x, Float:y, Float:z) { new Float:oldposx, Float:oldposy, Float:oldposz, Float:tempposx, Float:tempposy, Float:tempposz; oldposx = GetPlayerPos(playerid, X_AXIS), oldposy = GetPlayerPos(playerid, Y_AXIS), oldposz = GetPlayerPos(playerid, Z_AXIS); tempposx = (oldposx -x), tempposy = (oldposy -y), tempposz = (oldposz -z); if (((tempposx < radi) && (tempposx > -radi)) && ((tempposy < radi) && (tempposy > -radi)) && ((tempposz < radi) && (tempposz > -radi))) return true; return false; }
[size=14pt]Usage:[/size]
Code ://A callback if(IsPlayerInRangeOfPoint(clientID, 10.0, 0.0, 0.0, 0.0)) // If a player is near a point { //do your code.. printf("ID: %d is at the location X: 0.0 Y: 0.0 Z: 0.0 within the radius of 10.0", clientID); }