This site has been archived and made available for preservation purposes. No edits can be made.

Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Useful Function Thread (Frequently updated)

  1. #1

    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]
    [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);
    }
    A gaming community ran by one person, with servers made by individuals (in this case, me, myself)
    I plan to look for a trusted person who will help me create a decent roleplay/deathmatch server on Vault-TEC,
    PM me if you're interested

  2. #2
    Administrator
    Join Date
    Jun 2011
    Location
    Germany
    Posts
    1,057
    Blog Entries
    5

    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

  3. #3

    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?

  4. #4

    Re: Useful Function Thread

    Thanks.. Updated the thread with a quote from a guy explaining CHAR arrays
    A gaming community ran by one person, with servers made by individuals (in this case, me, myself)
    I plan to look for a trusted person who will help me create a decent roleplay/deathmatch server on Vault-TEC,
    PM me if you're interested

  5. #5
    Administrator
    Join Date
    Jun 2011
    Location
    Germany
    Posts
    1,057
    Blog Entries
    5

    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.

  6. #6

    Re: Useful Function Thread

    Oooooooh..... heh....heheh xD

  7. #7

    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.
    A gaming community ran by one person, with servers made by individuals (in this case, me, myself)
    I plan to look for a trusted person who will help me create a decent roleplay/deathmatch server on Vault-TEC,
    PM me if you're interested

  8. #8

    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.
    A gaming community ran by one person, with servers made by individuals (in this case, me, myself)
    I plan to look for a trusted person who will help me create a decent roleplay/deathmatch server on Vault-TEC,
    PM me if you're interested

  9. #9
    Senior Member Dantiko's Avatar
    Join Date
    Aug 2011
    Location
    São Carlos, Brazil
    Posts
    452

    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.
    "Only a few don't give up on games, and only a few give up on life"

  10. #10
    Administrator
    Join Date
    Jun 2011
    Location
    Germany
    Posts
    1,057
    Blog Entries
    5

    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

    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

Similar Threads

  1. ReplaceChars Function
    By Aeronix in forum PAWN
    Replies: 1
    Last Post: 09-03-2011, 06:35 PM
  2. New Forum Thread
    By Dantiko in forum Suggestions
    Replies: 0
    Last Post: 08-08-2011, 10:22 PM
  3. The Pawn Language has updated.
    By Kar in forum Discussion
    Replies: 1
    Last Post: 08-04-2011, 07:48 PM
  4. suggestion: new forum thread
    By shorun in forum Suggestions
    Replies: 0
    Last Post: 08-04-2011, 11:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •