Useful Functions

Quote:
Originally Posted by Kar
Посмотреть сообщение
the code finds a "_" right, if that "_" is found it sets it to " "

seems like ****** beat me

This forum requires that you wait 120 seconds between posts. Please try again in 60 seconds.
This forum requires that you wait 120 seconds between posts. Please try again in 42 seconds.
This forum requires that you wait 120 seconds between posts. Please try again in 23 seconds.
This forum requires that you wait 120 seconds between posts. Please try again in 1 seconds.
A better explanation would be that EOS is '/0' which means EOS (End Of String)
Reply

Quote:
Originally Posted by Zh3r0
Посмотреть сообщение
A better explanation would be that EOS is '/0' which means EOS (End Of String)
Isn't it \0?
Reply

Another version ( loops ), tested with Slice's benchmark code, it's faster.
( Also removed invalid character checks for the test on both function; nor the oneCharAllowed ).
pawn Код:
stock getPlayerFirstName( playerid )
{
    new
        playerName[ 24 ]
    ;

    GetPlayerName( playerid, playerName, 24 );

    for ( new i, nameLen = strlen( playerName ); i != nameLen; ++ i )
    {
        if ( playerName[ i ] == '_' && i > 0 )
        {
            playerName[ i ] = EOS;
        }
    }

    return playerName;
}
And thanks for the help!
Reply

You don't need all of that actually, the one with strfind was good. This here is even faster I guess:
pawn Код:
stock getPlayerFirstName(playerid)
{
    new
        szName[MAX_PLAYER_NAME],
        iPos
    ;
    GetPlayerName(playerid, szName, sizeof(szName));
   
    iPos = strfind(szName, "_");
   
    if(iPos != -1)
        szName[iPos] = EOS;
       
    return szName;
}
Reply

Quote:
Originally Posted by Kar
Посмотреть сообщение
Are you possibly thinking about making getvehiclepaintjob etc too? that would be nice ;\
must, beat, ryder!

Edit: I deem this impossible to script accurately because the OnVehiclePaintjob gets called if you view a piantjob according to the wiki, so it would mean that it sets the paintjob to whatever was viewed

Edit: after tough thinking i decided to just post the script I had already written, not yet tested, i left in the Modshop detection but comment it out. Thanks ryder because i had looked at what your script for the vehicle colors did to get some hints for this one.
pawn Код:
//GetPaintjob
new veh_paintjob_array[MAX_VEHICLES];
#define ChangeVehiclePaintjob(%1,%2); \
    veh_paintjob_array[(%1 - 1)] = %2; \
    ChangeVehiclePaintjob( %1, %2);
// Uncomment the following comment block to enable paintjob detection (Not very accurate) for piantjobs set in modshops
    /*
public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
{
    veh_paintjob_array[(vehicleid - 1)] = paintjobid;
        old_OnVehiclePaintjob( playerid, vehicleid, paintjobid);
    return 1;
}
forward old_OnVehiclePaintjob(playerid, vehicleid, paintjobid);
#define OnVehiclePaintjob old_OnVehiclePaintjob
*/

//stock GetVehiclePaintjob(vehicleid) { return veh_paintjob_array[(vehicleid - 1)]; }
#define GetVehiclePaintjob(%1); veh_paintjob_array[(%1 - 1)];
#define DestroyVehicle(%1, \
    veh_paintjob_array[(vehicleid - 1)] = 0;\
    DestroyVehicle( %1,
Edit.. again: changed the GetVehiclePaintjob to a define, bit quicker i think.
Reply

GetIP, faster than making variable, than use GetPlayerIp....
pawn Код:
stock GetIP(playerid)
{
    new PIP[16];
    GetPlayerIp(playerid, PIP, sizeof(PIP));
    return PIP;
}
Reply

Advise use variable type static or global variable, once when GetPlayerIp reset string to allocate the player andress ip
Reply

Quote:
Originally Posted by System64
Посмотреть сообщение
GetIP, faster than making variable, than use GetPlayerIp....
pawn Код:
stock GetIP(playerid)
{
    new PIP[16];
    GetPlayerIp(playerid, PIP, sizeof(PIP));
    return PIP;
}
Faster if you create a global variable instead local one.
Reply

A static variable is static on scope (), so, have a speed similar to global variable (is more slower, yes).
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
No, once compiled a static variable IS a global variable, the compiler just stops you from using it in other functions. This can be seen if you compile and decompile a mode - all statics will no longer be in their home functions.
So, she is static (global) in the current scope. It was not exactly what I said?
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
So the keyword static is pointless in pawn?

I may as well use global variables, or is the point of it to keep scripts ordered nicely, without having a huge list of global declarations at the top of the script?
They are useful.
Are not equal to global variables, because they are declared only in the current scope ..

pawn Код:
public OnPlayerSpawn(playerid)
{
    static
        lastPlayerSpawn
    ;
       
    printf("The last player on spawn is playerid %d", lastPlayerSpawn);
    return lastPlayerSpawn = playerid;
}
// It remains the old value and unreported in other scopes (only by declaring again)
The best definition would be:
Are global variables in a single scope (not the entire code)
Reply

Proof of Y-Less' Concept:
pawn Код:
#include <a_samp>

new globalVar1 = 1;
static globalStaticVar2 = 2;
main(){}

public OnFilterScriptInit()
{
    static staticVar3 = 3;
    staticVar3 = random(1000);
    doSomethingRandomWoohoo();
    printf("%d %d %d", globalVar1, globalStaticVar2, staticVar3);
    return 1;
}

stock doSomethingRandomWoohoo()
{
    globalVar1 = random(21000);
    globalStaticVar2 = random(210000);
    return 1;
}
decompiled:
pawn Код:
#include <a_samp>
#include <core>
#include <float>

new glob0 = 1;

new glob4 = 2;

new glob8 = 3;


main()
{
    return 0;
}

public OnFilterScriptInit()
{
    glob8 = random(1000);
    functionA8();
    printf("%d %d %d", 0, 4, 8);
    return 1;
}

functionA8()
{
    glob0 = random(21000);
    glob4 = random(210000);
    return 1;
}
Reply

Quote:
Originally Posted by [H]ead
Посмотреть сообщение
They are useful.
Are not equal to global variables, because they are declared only in the current scope ..

pawn Код:
public OnPlayerSpawn(playerid)
{
    static
        lastPlayerSpawn
    ;
       
    printf("The last player on spawn is playerid %d", lastPlayerSpawn);
    return lastPlayerSpawn = playerid;
}
// It remains the old value and unreported in other scopes (only by declaring again)

The best definition would be:
Are global variables in a single scope (not the entire code)
So the static is like "new" but its dont get resetted evrey time the callback is called?
Reply

Quote:
Originally Posted by [H]ead
Посмотреть сообщение
Random Player
Without params:
pawn Код:
getRandomPlayer()
{
    #if defined MAX_PLAYERS
        #undef MAX_PLAYERS
    #endif

    #define MAX_PLAYERS (DEFINE SLOTS HERE PLEASE)


    static
        p[MAX_PLAYERS]  
        a
    ;
   

    for(new i = 0, a = -1; i !=  MAX_PLAYERS; ++i) if(IsPlayerConnected(i))
    {
        p[++a] = i;    
    }

    return a == -1 ? -1 : p[random(a)]:
}
With params MAX and MIN playerid:
pawn Код:
getRandomPlayer(max, min)
{
    #if defined MAX_PLAYERS
        #undef MAX_PLAYERS
    #endif

    #define MAX_PLAYERS (DEFINE SLOTS HERE PLEASE)

    static
        p[MAX_PLAYERS]  
        a
    ;
    max++;

    for(a = -1; min !=  max; ++min) if(IsPlayerConnected(min))
    {
        p[++a] = min;    
    }

    return a == -1 ? -1 : p[random(a)]:
}
Cordially [H]ead
can someone explane this?
Reply

Quote:
Originally Posted by Rapgangsta
Посмотреть сообщение
So the static is like "new" but its dont get resetted evrey time the callback is called?
If the static variable has already been created the it isn't "Re Created" i think at compile time the var is actually placed outside of all the functions because it acts as a global var.
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
You could even do (though you don't have to):

pawn Код:
#define loadfs%0; LoadFS(#%0);

loadfs iradio;
That's the closest I think you can get it to typing as in a console, or:

pawn Код:
#define loadfs%0"%1" LoadFS(#%1);

loadfs "iradio"
A little closer to your version and using standard strings without the <> or ;.
Thanks I did't know that attaching %0 to loadfs without space will work even with a space
(Sorry for bad grammar XD)

Quote:
Originally Posted by HotPlayer
Посмотреть сообщение
why you post in OLD threads this is not think
See the Date of create!
-.-

Quote:
Sticky: Useful Functions

Sticky means that an admin put it on the top of the page because it hasn't to be forgotten!
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
pawn Код:
stock GetCenterOfGangzone(Float:minx, Float:miny, Float:maxx, Float:maxy, &Float:retx, &Float:rety)
{
    retx = (maxx - minx) / 2 + minx;
    rety = (maxy - miny) / 2 + miny;
}
retx and rety is where it stores the value, correct?
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
The PAWN implemetors guide, and modes compiled with the "-a" flag (output assembly instead of .amx).
I've edited settings.ini in the pawno folder and changed params=-r to params=-a but it still compiles as AMX and not as assembly, what the heck?
Reply

In cmd.exe:

pawncc.exe JunkBuster.pwn -a

(moved JunkBuster.pwn to pawno folder)

Result:
http://www.sendspace.com/file/135jk9
Reply

Quote:
Originally Posted by Double-O-Seven
Посмотреть сообщение
In cmd.exe:

pawncc.exe JunkBuster.pwn -a

(moved JunkBuster.pwn to pawno folder)

Result:
http://www.sendspace.com/file/135jk9
You should also put these:
Код:
-; -(
Those are used in PAWNO, by default. Without them, you might get really strange errors.
Reply


Forum Jump:


Users browsing this thread: 35 Guest(s)