How to equal value to constants from the list?
#1

Hi,
I'm creating my first script to enable unoccupied vehicles getting damaged, in the same way as if there is a driver. Found few scripts, but they don't provide an actual car damage like when there is driver. So I've run some tests to get exact values, but found problem on how to check if vehicle is airplane, as same guns deal two different damage to cars and airplanes.

I need to check if vehicleid is equal to one from the airplane list:
Код:
new AirVehID[] = {460, 476, 511, 512, 513, 519, 520, 553, 577, 592, 593, 417, 425, 447, 469, 487, 488, 497, 548, 563};

if(vehicleid == What to put here?)
How can I do this? Thanks in advance.
Reply
#2

pawn Код:
IsVehicleOnList(vehicleid)
{
    new idx = 0;
    switch(GetVehicleModel(vehicleid))
    {
        case 460, 476, 511, 512, 513, 519, 520, 553, 577, 592, 593, 417, 425, 447, 469, 487, 488, 497, 548, 563: idx = 0;
        default: idx = 0;
    }
    return idx;
}

IsVehicleOnList_2(modelid)
{
    static const AirVehID[] = {460, 476, 511, 512, 513, 519, 520, 553, 577, 592, 593, 417, 425, 447, 469, 487, 488, 497, 548, 563};

    for(new i = sizeof(AirVehID) - 1; i > -1; i--)
        if(AirVehID[i] == modelid)
            return 1;

    return 0;
}
You can choose, in second example you need add GetVehicleModel
pawn Код:
if(IsVehicleOnList_2(GetVehicleModel(vehicleid)))
Reply
#3

I've code it a little differently, but you got me the idea, so thanks and +1 for you

Код:
new vModel = GetVehicleModel(vehicleid);
if(vModel == (460, 476, 511, 512, 513, 519, 520, 553, 577, 592, 593, 417, 425, 447, 469, 487, 488, 497, 548, 563))
{

}
Reply
#4

That is completely invalid and will probably just result in "true" all the time, even if the vehicle model isn't on that list. Use this:

PHP код:
stock in_array(needle, const haystack[], size sizeof haystack, &index 0)
{
    for(new 
isizei++)
    {
        if(
haystack[i] == needle)
        {
            
index i;
            return 
true;
        }
    }
        
    return 
false;

Reply
#5

Yeah it's invalid, it returns true all the time, like you said. I'm not quite sure why it worked at the first when I tested it. But I don't understand your code, can you explain it? I'm new at PAWN, and don't know quite much still. I have no clue what's needle and haystack for.
Reply
#6

Just use something like
PHP код:
IsAirplane(model)
{
    switch(
model)
    {
        case 
460476511512513519520553577592593417425447469487488497548563: return 1;
    }
    return 
0;
}
// If the player's vehicle is an airplane
if (IsAirplane(GetVehicleModel(GetPlayerVehicleID(playerid))) == 1)
{
    ...
}
// Or, if it's too difficult to follow the ():
new vehicleid GetPlayerVehicleID(playerid);
new 
model GetVehicleModel(vehicleid);
if (
IsAirplane(model) == 1)
{
    ...

Reply
#7

Yeah, got it, thanks! I've found something similar in some old post, and use it. But I thought that I'll need multiple switches to make this possible on this way and gave up on that and used the "if" instead of "switch". But know you explained me more clearly and showed me how can I bypass multiple switches and use one simple function instead. Thanks again and +1 for you
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)