Useful Functions

~~~~
Reply

~~~~
Reply

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
i've proved myself wrong, better stop posting functions emmet!
Those people have an obsession with optimisation .



Do not do what they want if you really think that your code is fine .



I hope that I have helped .
Reply

Quote:
Originally Posted by rjjj
Посмотреть сообщение
Those people have an obsession with optimisation .
Right, why should optimization be important? Fuck that shit, yolo, we're much better off buying new computers to play Minesweeper right?
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
If you return the string directly, you can't specify an array size, it's constrained to the size defined in the array declaration in the function!

pawn Код:
print(randomString(12));
You may think that's easier to write but I think flexibility is more important:

pawn Код:
new txt[400];
randomString(txt, 400);
print(txt);
pawn Код:
new txt[2];
randomString(txt, 2);
print(txt);
Etc...


I used to think the direct return was good, until I ran into an array size problem with my file script.

It used to do this: string = file_GetStr("key");
But that pre-defined the return string as 128 cells, no more, no less.
So now it works like this: file_GetStr("key", string);
Which also means I can do stuff like:
pawn Код:
new name[24];
file_GetStr("PlayerName", string);
// I'm getting a player name from a file, it's no more than 24 characters!

new banreason[128];
file_GetStr("PlayerBanReason", banreason);
// Now I'm getting something a little longer
The old system would have complained:
pawn Код:
new name[24];
name = file_GetStr("PlayerName");
// Error: "Array sizes do not match or destination array is too small"
Understand why I pointed that out now?
Yea, thanks.
"But that pre-defined the return string as 128 cells, no more, no less."
But actually, functions can return strings less than defined array size.
For example
pawn Код:
string_return()
{
new string[500];
format(string, 500, "Hello world!");
return string;
}
main()
{
new string[13];
format(string, 13, "%s", string_return());
print(string);
}
will return correct string;
Reply

A simple function which detects if player is spawned. If you use this, please add those codes under your includes. (On every scripts you use.)
pawn Код:
new PlayerIsSpawned[MAX_PLAYERS];
stock IsPlayerSpawned(playerid)
{
 if(PlayerIsSpawned[playerid] == 1) return true;
 else return false;
}

public OnPlayerSpawn(playerid)
{
 PlayerIsSpawned[playerid] = 1;
 CallLocalFunction("L_OPS", "d", playerid);
 return 1;
}

public OnPlayerConnect(playerid)
{
 PlayerIsSpawned[playerid] = 0;
 CallLocalFunction("L_OPC", "d", playerid);
 return 1;
}

#if defined _ALS_OnPlayerSpawn
 #undef OnPlayerSpawn
#else
#define _ALS_OnPlayerSpawn
#endif
#define OnPlayerSpawn L_OPS

#if defined _ALS_OnPlayerConnect
 #undef OnPlayerConnect
#else
#define _ALS_OnPlayerConnect
#endif
#define OnPlayerConnect L_OPC

forward L_OPS(playerid);
forward L_OPC(playerid);
Example:

pawn Код:
CMD:suicide(playerid, params[])
{
 if(IsPlayerSpawned(playerid))
 {
  SetPlayerHealth(playerid, 0.0);
 }
 else
 {
  SendClientMessage(playerid, 0xFF0000FF, "You must be spawned to use this command.");
 }
 return 1;
}
Reply

Quote:
Originally Posted by ******
Посмотреть сообщение
That code never resets to unspawned.
Thanks for pointing it out, edited the previous post.
Reply

Quote:
Originally Posted by Lordz™
Посмотреть сообщение
Thanks for pointing it out, edited the previous post.
still the same. You need reset spawned value to zero, when player disconnect.
Reply

https://sampwiki.blast.hk/wiki/GetPlayerState

Just use states, if the states are "not spawned" type (0, 7, 9), return 0 in IsPlayerSpawn, else, 1.
Reply

I set the value to '1' while player gets spawned and when a player gets connected, it sets to '0'. I actually meant the function IsPlayerSpawned as is a player spawned on server or not.
Reply

pawn Код:
stock bool:IsPlayerSpawned(playerid) {
        new pstate = GetPlayerState(playerid);
        if((pstate >= PLAYER_STATE_ONFOOT && pstate <= PLAYER_STATE_ENTER_VEHICLE_PASSENGER) || pstate == PLAYER_STATE_SPAWNED) return true;
        return false;
}
Reply

pawn Код:
stock IsValidObject(value) {
    static const valid_values[563] = {
        -1, -4030465, -1048577, -1, -1, -33292289, -1, -1, -1, -113, -1073741825, -1, -2, -1, -1, -1, -1, -1, -65529, -1, -1, -1, -129, -1, -1, -1, -1, -1, -1, -1073741825, -1, -1, -1, -268435457, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -838598657, -1, -1, -1, -1, -1, -1, -1, 25166335, -16777216, 266249, 16512, -134255428, -1, -1, -24577, -1, -1073872897, -1, -1, -1, -1, -1, -1572865, -1, -1, -1, -1, -1, -3, -1, -25165825, -1041, -1, 2147483647, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2139095041, -1, -1, -1, -1, -1, -1, 1048575, -2147483648, -1, -1, -1, -1, -1, -1, -1, -1, 130023423, -1024, -1, -1, -1, -1, -1, -1, -1, 33554431, -128, -1, -1, -1, -1, -1, -1, -1, -1, 4095, -1, -1, -1, -1, -1, -1, -1, -1, -1, -3145729, -65, -1, -1, -1, -1, -1, -1, 1919, -2, -1, -33, -1, -1, -1, -1, 8388607, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -256, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 67108863, -65536, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2147483647, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -16377, -1, -8193, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -8126465, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1572865, -1, -1, -1, -1, -1, -1, -1, 134217727, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -33554432, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2097151, -32768, -1, 536870911, -2, -1, -1, -1, -1, -1, 268435455, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -256, -1, -1, -1, -67108865, -5242881, -1, -1, -403169281, -1, -15, -253, -1921, -1, -1, -536870917, 126975, 0, 0, 0, -1024, 262143, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -33554432, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 65535, 0, 0, 0, 0, 0, 0, -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 268435455, -2097152, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 720895, -512, -16385, -1, 38207487, 0, 0, -131072, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
    };

    if (615 <= value <= 18630) {
        value -= 615;


        return (valid_values[value >>> 5] & (1 << (value & 31))) || false;
    }

    return false;
}
1443 line array converted into a bit array. Once again, thanks Slice.

It is an array that I took from a 2007 file, so it most likely will not support SA-MP custom objects.
Reply

Where is this bit array tool I allegedly created?!
Reply

Meh, you supplied the link so then I assume you created it as you are a web freak :P

http://forum.sa-mp.com/showpost.php?...&postcount=822

It was four months back, and I will never forget such a useful thing :P
Reply

Код:
stock RemovePlayerWeapon(playerid, ...)
{
	new Args = numargs();
	for(new i = 1; i < Args; i++)
	{
		SetPlayerAmmo(playerid, getarg(i), 0);
	}
	return 1;
}
Remove player weapons!
Example:
PHP код:
RemovePlayerWeapon(playerid2625); 
Remove player weapons: Sawn-Off, Shotgun.
Reply

Shouldn't it work with slots? You cannot have a sawnoff and a shotgun at the same time.
Reply

Quote:
Originally Posted by Jochemd
Посмотреть сообщение
Shouldn't it work with slots? You cannot have a sawnoff and a shotgun at the same time.
SetPlayerAmmo works with a weapon ID rather than slot, so this function makes sense - it was just a bad example.
Reply

pawn Код:
stock IsPaintjobVehicle(value) {
    static const valid_values[3] = {
        1, 3670016, 806680576
    };

    if (483 <= value <= 576) {
        value -= 483;


        return (valid_values[value >>> 5] & (1 << (value & 31))) || false;
    }

    return false;
}
576, 575, 567, 565, 562, 561, 560, 559, 558, 536, 535, 534, 483

value = model id

IsPaintJobVehicle( GetVehicleModel( GetPlayerVehicleID( playerid ) ) )
Reply

pawn Код:
stock GetVehiclePAWNData(const vehicleid)
{
    new TempStr[650];
    new Float:_Pos[3],
        Float:_Angle;
    GetVehiclePos(vehicleid, _Pos[0], _Pos[1], _Pos[2]);
    GetVehicleZAngle(vehicleid, _Angle);
    format(TempStr, 130, "new const v = CreateVehicle(%d, %f, %f, %f, %f, COLOUR_1, COLOUR_2, RESPAWN_TIME);", GetVehicleModel(vehicleid), _Pos[0], _Pos[1], _Pos[2], _Angle);
    new TempLine[60], c;
    new const SlotName[14][24] =
    {
        "CARMODTYPE_SPOILER",
        "CARMODTYPE_HOOD",
        "CARMODTYPE_ROOF",
        "CARMODTYPE_SIDESKIRT",
        "CARMODTYPE_LAMPS",
        "CARMODTYPE_NITRO",
        "CARMODTYPE_EXHAUST",
        "CARMODTYPE_WHEELS",
        "CARMODTYPE_STEREO",
        "CARMODTYPE_HYDRAULICS",
        "CARMODTYPE_FRONT_BUMPER",
        "CARMODTYPE_REAR_BUMPER",
        "CARMODTYPE_VENT_RIGHT",
        "CARMODTYPE_VENT_LEFT"
    };
    for(new i = 0; i < 14; i++)
    {
        c = GetVehicleComponentInSlot(vehicleid, i);
        if(c != 0)
        {
            format(TempLine, 60, "\r\nAddVehicleComponent(v, %d); // %s", c, SlotName[i]);
            strcat(TempStr, TempLine);
        }
    }
    return TempStr;
}
The function returns vehicle's "source code", for instance something like this:

Код:
new const v = CreateVehicle(600, 2394.272705, 1034.542236, 10.514182, 140.348159, COLOUR_1, COLOUR_2, RESPAWN_TIME);
AddVehicleComponent(v, 1004); // CARMODTYPE_HOOD
AddVehicleComponent(v, 1006); // CARMODTYPE_ROOF
AddVehicleComponent(v, 1007); // CARMODTYPE_SIDESKIRT
AddVehicleComponent(v, 1013); // CARMODTYPE_LAMPS
AddVehicleComponent(v, 1010); // CARMODTYPE_NITRO
AddVehicleComponent(v, 1020); // CARMODTYPE_EXHAUST
AddVehicleComponent(v, 1097); // CARMODTYPE_WHEELS
AddVehicleComponent(v, 1087); // CARMODTYPE_HYDRAULICS
Reply

Hi guys,this is my function from my GM from 0.

I made for me because in my GM isn't defined

-Its SetPlayerMoney

Код:
stock SetPlayerMoney(playerid, cash)
{
  ResetPlayerMoney(playerid);
  return GivePlayerMoney(playerid, cash);
}
Reply


Forum Jump:


Users browsing this thread: 6 Guest(s)