Useful Function Thread (Frequently updated)
[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]
Quote:
[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;
// ^ ^
}
[size=18pt]GetPlayerPosFixed[/size]
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);
}
Re: Useful Function Thread
[quote author=Lorenc link=topic=33.msg129#msg129 date=1309070914]Which I am sorry if I am not allowed to do so.[/quote]
Stickied, thanks for creating such a thread ;)
[quote author=Lorenc link=topic=33.msg129#msg129 date=1309070914]// 24 Characters, I don't know the limit yet...[/quote]
16 characters, but I might raise this limit later. ReturnPlayerName is indeed a nice snippet, should work without any problem ;)
Code :
#define MAX_PLAYERS 24 // Server slots
#define Variable:%1<%2> %1[%2 char]
new
Variable: Hey < MAX_PLAYERS >;
Never tried that myself, but if it works, it would in fact be very efficient. Thanks for the hint :D
Re: Useful Function Thread
16 player Limit? I saw the test server with 32 player limit. Though It said not to use and use the other servers... I was pretty tempted to join anyway when I saw 2 people already in xD
EDIT: Which btw, How did that work out?
Re: Useful Function Thread
Thanks.. Updated the thread with a quote from a guy explaining CHAR arrays
Re: Useful Function Thread
[quote author=ArathHunter link=topic=33.msg165#msg165 date=1309125715]16 player Limit?[/quote]
16 characters per player name ;) The max-playercount is unknown up until now.
Re: Useful Function Thread
Oooooooh..... heh....heheh xD
Re: Useful Function Thread
Added a upload of the compiler set, contains pawno.exe, the main IDE of PAWN and includes new.pwn along with the includes.
Re: Useful Function Thread
Another function added: GetPlayerPosFixed(playerid, &Float:X, &Float:Y, &Float:Z)
The design of the thread has been changed (colors) for a much neater and understandable look.
Re: Useful Function Thread (Frequently updated)
Umm, the 32 player server is 999 ping, too laggy.Well its only test, the internet is supposed to suck because we don't have servers around the globe yet.
Re: Useful Function Thread (Frequently updated)
999 ping usually means that the mod failed to ping the server (I have to change that 999 to something else). The only "official" servers are those named "vaultmp 0.1a testserver", you should be able to properly ping them :D
BTW, the next revision will come with the updated version of PAWN. Scripters may want to check this: https://www.compuphase.com/pawn/pawnhistory.htm