Useful Snippets

Quote:
Originally Posted by [FeK]DraKiNs
View Post
Yeap, it is better to use PVARS


(In most of the Examples is useless.) But for me, get a more beautiful code

Lorenc:
Your code will not work with variations of hacking (Example: 9999999.999 to 99999.99999 )
You can use || ('or' operator) but this cleo has massive integers installed.. Anyway, your one wouldn't work with different variations as well.

0xDBB9F = 899999

0xF423F = 999999 (Which is what I have, except with some decimal numbers)

Hexidecimal is neat. Anyway, about PVars, I don't use one PVar inside my gamemode, and don't want to lol. Char arrays, and other techniques keep me away from it.
Reply

Quote:
Originally Posted by ******
View Post
PVars are both slower and larger in memory footprint. There are VERY few reasons for using them over well-designed code.
PVars are dynamic memory, you'll easily notice better speed in the compilation. But really, not worth using them if you take into account the speed. But I think they're very flexible for those who do not have time to let the "perfect code". Do you agree?

In "script x script" is quite feasible using CallRemoteFunction.

Quote:

You can use || ('or' operator) but this cleo has massive integers installed.. Anyway, your one wouldn't work with different variations as well.

0xDBB9F = 899999

0xF423F = 999999 (Which is what I have, except with some decimal numbers)

Hexidecimal is neat. Anyway, about PVars, I don't use one PVar inside my gamemode, and don't want to lol. Char arrays, and other techniques keep me away from it.

So we need put a value LESS.
Reply

I can't be bothered going in-game to test, but I'll only say that 20,000 should be fine also.
Reply

Quote:
Originally Posted by [HLF]Southclaw
View Post
Seems PVars are just for convenience really, all the advantages (apart from that one about holding large strings) are purely to make things easier (Auto delete, no need for IsPlayerConnected, cross-script use etc)


Also, like ****** said about compile time checking, what if you misspell a PVar? Since they are strings not labels an error like this would be annoying!


Anyway, I really should get this back on topic by posting a useful snippet... Can't think of any that I haven't posted already though
This is what I'm talking about

---

Slice released code similar to "PVARS". It pays to use it?
Reply

Quote:
Originally Posted by BaubaS
Посмотреть сообщение
Hey, is it possible to make sunch function like "IsCarFalling"? Because I dont think checking Z coordinate is the best way, as there are some roads in high, also mt chilliad.
Velocity is the only possible way afaik (vehicle velocity of the Z axis).
Reply

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
pawn Код:
new
    firecat_makeitglobal[ 150 ]; // I don't even know 150 is the limit...
    bool: FALSE = false // y u no make it bool
;
#define GameTextForPlayerEx(%0,%1,%2,%3,%4)                             do{format(firecat_makeitglobal,150,%1,%4); GameTextForPlayer(%0,firecat_makeitglobal,%2,%3);}while(FALSE)
Usage
Код:
GameTextForPlayerEx(playerid,"Firecat still lives in 2007",3500,3,playerid);
Why would you move a perfectly good local to a global?
Reply

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
pawn Код:
new
    firecat_makeitglobal[ 150 ]; // I don't even know 150 is the limit...
    bool: FALSE = false // y u no make it bool
;
#define GameTextForPlayerEx(%0,%1,%2,%3,%4)                             do{format(firecat_makeitglobal,150,%1,%4); GameTextForPlayer(%0,firecat_makeitglobal,%2,%3);}while(FALSE)
Usage
Код:
GameTextForPlayerEx(playerid,"Firecat still lives in 2007",3500,3,playerid);
Your example makes no sense, because you're not using params.
As Y_Less asked, why did you turn a local variable to global?
Reply

Also, that code won't work with no format parameters.
Reply

It also won't compile because variable declaration contains space at invalid position.
Reply

Quote:
Originally Posted by Y_Less
Посмотреть сообщение
Also, that code won't work with no format parameters.
So I guess he didn't test it...
Reply

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
Velocity is the only possible way afaik (vehicle velocity of the Z axis).
Can you give me an example? Because I suck at maths, especially such formulas
Reply

Quote:
Originally Posted by BaubaS
Посмотреть сообщение
Can you give me an example? Because I suck at maths, especially such formulas
No need for formulas
https://sampwiki.blast.hk/wiki/GetVehicleVelocity
Store the z axis, and check it for example if(z > 1)...
Because z is height
Reply

I thought velocity works a bit different. But will this really work when player for example is on ch. mountain?
Reply

Velocity has nothing to do with where you are. It has to do with how fast you're moving, and in which direction.
Reply

Quote:
Originally Posted by Kwarde
Посмотреть сообщение
But why would you use this GameTextForPlayerEx if you're not using the format parameters? However, as mentioned before, why using those global variables? I always use these (these were all tested )
pawn Код:
#define SendMessage(%0,%1,%2,%3)            do{new smStr[128];format(smStr,128,%2,%3);SendClientMessage(%0,%1,smStr);}while(FALSE)
#define GameTextForPlayerEx(%0,%1,%2,%3,%4) do{new gTFPE[150];format(gTFPE,150,%1,%4);GameTextForPlayer(%0,gTFPE,%2,%3);}while(FALSE)
#define GameTextForAllEx(%0,%1,%2,%3)       do{new gTFA[150];format(gTFA,150,%0,%3);GameTextForAll(gTFA,%1,%2);}while(FALSE)

stock bool:FALSE = false;
I always use those variables inside the do{} stuff.

WARNING: You cannot use these function things in a return statement thing.
Example, this will not work:
pawn Код:
if (!IsPlayerConnected(id)) return SendMessage(playerid, 0xFF0000AA, "Playerid %d is not connected!", id);
Why? Before PAWNO is going to compile your gamemode, it will first change these functions as how it is in the #define. So this code above actually will look like this:
pawn Код:
if (!IsPlayerConnected(id)) return do{new smStr[128];format(smStr,128,"Playerid %d is not connected!,id);SendClientMessage(playerid,0xFF0000AA,smStr);}while(FALSE)
If you try to compile both these codes, you'll get the same errors (except when you don't define the SendMessage off course)
heres a version of all, that do support returns, and do support no parameters(and also includes a "SendMessageToAll").


Functions:
pawn Код:
GameTextForPlayerEx(playerid, string[], time, style, {Float,_}:...);
GameTextForAllEx(string[], time, style, {Float,_}:...);
SendMessage(playerid, color, message[], {Float,_}:...);
SendMessageToAll(color, message[], {Float,_}:...);
Code:
pawn Код:
#if !defined FMsg
    stock FMsg[400];
#endif

stock GameTextForPlayerEx(playerid,const string[],time,style)return GameTextForPlayer(playerid,string,time,style);
#define GameTextForPlayerEx(%0,%1,%2,%3,%4) format(FMsg, sizeof(FMsg),%1,%4), GameTextForPlayer(%0,FMsg,%2,%3)

stock GameTextForAllEx(const string[],time,style)return GameTextForAll(string,time,style);
#define GameTextForAllEx(%0,%1,%2,%3) format(FMsg, sizeof(FMsg),%0,%3), GameTextForAll(FMsg,%1,%2)

#define SendMessage(%0,%1,%2) format(FMsg, sizeof(FMsg), %2), SendClientMessage(%0,%1,FMsg)
#define SendMessageToAll(%0,%1) format(FMsg, sizeof(FMsg), %1), SendClientMessageToAll(%0,FMsg)
Reply

This stock functions will check if if vehicle is a boat or an airplane/helicopter

pawn Код:
stock IsWaterVehicle(model)
{
    switch(model)
    {
        case 472:{ return 1;}
        case 473:{ return 1;}
        case 493:{ return 1;}
        case 595:{ return 1;}
        case 484:{ return 1;}
        case 430:{ return 1;}
        case 453:{ return 1;}
        case 452:{ return 1;}
        case 446:{ return 1;}
        case 454:{ return 1;}
        case 460:{ return 1;}
        default: return 0;
    }
    return 0;
}

stock IsAirVehicle(model)
{
    switch(model)
    {
        case 592:{ return 1;}
        case 577:{ return 1;}
        case 511:{ return 1;}
        case 512:{ return 1;}
        case 593:{ return 1;}
        case 520:{ return 1;}
        case 553:{ return 1;}
        case 476:{ return 1;}
        case 519:{ return 1;}
        case 513:{ return 1;}
        case 548:{ return 1;}
        case 425:{ return 1;}
        case 417:{ return 1;}
        case 487:{ return 1;}
        case 497:{ return 1;}
        case 488:{ return 1;}
        case 563:{ return 1;}
        case 447:{ return 1;}
        case 469:{ return 1;}
        default: return 0;
    }
    return 0;
}
Reply

Quote:
Originally Posted by Makaveli93
Посмотреть сообщение
This stock functions will check if if vehicle is a boat or an airplane/helicopter
-code-
I wrote something like that earlier ago (1 week) however I didn't share it publicly.

pawn Код:
stock IsBoatVehicle( model )
{
    static const
        BoatVehicles[ ] = {
            472, 473, 595, 493, 430, 453, 484, 446, 452, 454
        }
    ;

    for( new i; i < sizeof( BoatVehicles ); i++ ) {
        if( BoatVehicles[ i ] == model ) return 1;
    }
    return 0;
}

stock IsAirVehicle( model )
{
    static const
        AirVehicles[ ] = {
            577, 511, 592, 520, 593, 512, 476, 553, 460, 519, 548, 425, 488, 487, 417, 563, 497, 447, 469
        }
    ;
   
    for( new i; i < sizeof( AirVehicles ); i++ ) {
        if( AirVehicles[ i ] == model ) return 1;
    }
    return 0;
}
They're functions anyway...
Reply

pawn Код:
stock IsPlayerFemale(Model) {
    if(Model < 0 || Model > 299) {
        return false;
    }

    static const Female[] = {
        9, 10, 11, 12, 13, 31, 38, 39, 40, 41, 53, 54, 55, 56, 63, 64, 65, 69, 75, 76, 77, 85, 87, 88, 89, 90, 91, 92, 93, 129, 130, 131, 138, 139, 140, 141, 145, 148, 150, 151, 152, 157, 169, 172,
        178, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 201, 205, 207, 211, 214, 215, 216, 218, 219, 224, 225, 226, 231, 232, 233, 237, 238, 243, 244, 245, 246, 251, 256, 257, 263, 298
    };

    for(new FemaleID = 0; FemaleID < sizeof(Female); FemaleID++) {
        if(Female[FemaleID] == Model) {
            return true;
        }
    }
    return false;
}

stock IsPlayerSkin(Model) {
    if(IsPlayerMale(Model) || IsPlayerFeamle(Model)) {
        return true;
    }
    return false;
}
Report the missing or misplaced skins by sending me a private message please.
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
Could you post an example of a bit array?
I'm not really sure what the term means, I thought it meant a single 32 bit variable used for 32 boolean values.

Are they those "IsThisASomething" functions where you have an array of hex numbers then an if statement with some bitwise shifting and stuff... It's hard to explain, I've seen some posted before, I never understood them and I don't really know what keywords to use to search (apart from "bit array" but I didn't really find anything)

I may as well ask while it's on topic!
It's an extension of storing 32 bools in one variable - it stores 32*x bools in an array. When reading bits 1-32, the first cell is used, 32-64 second cell, etc. So for example, slot 123 would be the 27th bit in bit_array[3].

Yes, they're what you've seen in some IsThisASomething. One HUGE example is g_baValidModels in hold-studio.pwn. For readability, I used binary notation instead of numbers or hex.
Reply

Quote:
Originally Posted by [HLF]Southclaw
Посмотреть сообщение
Could you post an example of a bit array?
I'm not really sure what the term means, I thought it meant a single 32 bit variable used for 32 boolean values.

Are they those "IsThisASomething" functions where you have an array of hex numbers then an if statement with some bitwise shifting and stuff... It's hard to explain, I've seen some posted before, I never understood them and I don't really know what keywords to use to search (apart from "bit array" but I didn't really find anything)

I may as well ask while it's on topic!
I'm not quite sure if that's what bit arrays actually are. I personally have no idea how I could compress an array more than by using char arrays if it needs to contain values greater than 1, in this case. Like:
pawn Код:
enum type
{
    TYPE_UNKNOWN,
    TYPE_LAND,
    TYPE_WATER,
    // ...
}

new vehicleTypes[212 char] = { 16777216 /* 1 << 24 */, ... };
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)