Using arrays?
#1

I've got this:
pawn Код:
new CGLSVehicles[23];
Then I created my vehicles like this:"
pawn Код:
CGLSVehicles[1] = AddStaticVehicleEx(472,-2954.7998047,491.1992188,0.0000000,63.9953613,6,-1,15); //Coastguard
    CGLSVehicles[2] = AddStaticVehicleEx(472,-2954.8000488,495.2999878,0.0000000,64.0000000,6,53,15); //Coastguard
    CGLSVehicles[3] = AddStaticVehicleEx(472,-2941.3000488,491.6000061,0.0000000,310.0000000,6,53,15); //Coastguard
    CGLSVehicles[4] = AddStaticVehicleEx(472,-2955.0000000,499.2999878,0.0000000,63.9953613,6,53,15); //Coastguard
    CGLSVehicles[5] = AddStaticVehicleEx(472,-2955.3000488,503.5000000,0.0000000,63.9953613,6,-1,15); //Coastguard
    CGLSVehicles[6] = AddStaticVehicleEx(472,-2941.2998047,495.8994141,0.0000000,309.9957275,6,-1,15); //Coastguard
    CGLSVehicles[7] = AddStaticVehicleEx(472,-2940.6992188,501.2998047,0.0000000,309.9957275,6,-1,15); //Coastguard
    CGLSVehicles[8] = AddStaticVehicleEx(472,-2941.0996094,505.5996094,0.0000000,309.9957275,6,-1,15); //Coastguard
    CGLSVehicles[9] = AddStaticVehicleEx(430,-2981.3999023,491.2999878,0.0000000,2.0000000,-1,103,15); //Predator
    CGLSVehicles[10] = AddStaticVehicleEx(430,-2971.1000977,491.5000000,0.0000000,357.9978027,-1,103,15); //Predator
    CGLSVehicles[11] = AddStaticVehicleEx(430,-2980.6999512,508.0000000,0.0000000,359.9978027,-1,103,15); //Predator
    CGLSVehicles[12] = AddStaticVehicleEx(430,-2970.8999023,507.5000000,0.0000000,357.9949951,-1,103,15); //Predator
    CGLSVehicles[13] = AddStaticVehicleEx(490,-2949.3999023,458.7999878,5.1999998,334.0000000,6,-1,15); //FBI Rancher
    CGLSVehicles[14] = AddStaticVehicleEx(490,-2954.1999512,458.7999878,5.1999998,333.9953613,6,-1,15); //FBI Rancher
    CGLSVehicles[15] = AddStaticVehicleEx(490,-2958.5000000,459.5000000,5.1999998,333.9953613,6,-1,15); //FBI Rancher
    CGLSVehicles[16] = AddStaticVehicleEx(490,-2944.5996094,459.0000000,5.1999998,333.9953613,6,-1,15); //FBI Rancher
    CGLSVehicles[17] = AddStaticVehicleEx(597,-2971.3994141,458.6992188,4.6999998,337.9998779,7,1,15); //Police Car (SFPD)
    CGLSVehicles[18] = AddStaticVehicleEx(597,-2978.6999512,458.6000061,4.6999998,337.9998779,7,1,15); //Police Car (SFPD)
    CGLSVehicles[19] = AddStaticVehicleEx(597,-2975.3000488,458.6000061,4.6999998,337.9998779,7,1,15); //Police Car (SFPD)
    CGLSVehicles[20] = AddStaticVehicleEx(597,-2982.1999512,458.3999939,4.6999998,337.9998779,7,1,15); //Police Car (SFPD)
    CGLSVehicles[21] = AddStaticVehicleEx(497,-2969.3000488,479.6000061,5.0000000,0.0000000,7,1,15); //Police Maverick
    CGLSVehicles[22] = AddStaticVehicleEx(487,-2954.1999512,479.5000000,5.0000000,0.0000000,6,-1,15); //Maverick
Now how can I use all of these vehicles at once?
pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(GetPlayerVehicleID(playerid) == CGLSVehicles[]) // LINE 656
    {
   
    }
    return 1;
}
I tried this but I get this error:
pawn Код:
(656) : error 029: invalid expression, assumed zero
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase


1 Error.
Reply
#2

You need to ****** how to traverse arrays! For this, you need the "for loop"

pawn Код:
for(new counter = 0; counter < sizeof(CGLSVehicles); counter++)
{
      if(GetPlayerVehicleID(playerid) == CGLSVehicles[counter])
      {
              // code
      }
}
counter is a variable that gets incremented at each iteration until it reaches the maximum value of the array limit (here, 22) and it will stop (THAT ITERATION won't get executed.) Make sure you use 'break;' after your statement to get OUT of the loop. If you want to skip a particular run (iteration) use continue.
Reply
#3

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    for(new i; i < sizeof(CGLSVehicles); i++)
    {
        if(GetPlayerVehicleID(playerid) == CGLSVehicles[i]) // LINE 656
        {
            //Do stuff, they're in one of your vehicle!
            break; //Stop the loop, we don't need it anymore.
        }
    }
    return 1;
}
Reply
#4

Quote:
Originally Posted by Ash.
Посмотреть сообщение
pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    for(new i; i < sizeof(CGLSVehicles); i++)
    {
        if(GetPlayerVehicleID(playerid) == CGLSVehicles[i]) // LINE 656
        {
            //Do stuff, they're in one of your vehicle!
            break; //Stop the loop, we don't need it anymore.
        }
    }
    return 1;
}
Well that will work but you don't want to keep calling the same function every iteration it should actually look like this.

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    vid = GetPlayerVehicleID(playerid);
    for(new i; i < sizeof(CGLSVehicles); i++)
    {
        if(vid == CGLSVehicles[i]) // LINE 656
        {
            //Do stuff, they're in one of your vehicle!
            break; //Stop the loop, we don't need it anymore.
        }
    }
    return 1;
}
I'd also make a dynamic car loader instead of sequentially incrementing the array during vehicle creation.
Reply
#5

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
Well that will work but you don't want to keep calling the same function every iteration it should actually look like this.

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    new veh = GetPlayerVehicleID(playerid);
    for(new i; i < sizeof(CGLSVehicles); i++)
    {
        if(veh== CGLSVehicles[i]) // LINE 656
        {
            //Do stuff, they're in one of your vehicle!
            break; //Stop the loop, we don't need it anymore.
        }
    }
    return 1;
}
I'd also make a dynamic car loader instead of sequentially incrementing the array during vehicle creation.
By the way, it's:

pawn Код:
public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    for(new i; i < sizeof(CGLSVehicles); i++)
    {
        if(vehicleid == CGLSVehicles[i]) // LINE 656
        {
            //Do stuff, they're in one of your vehicle!
            break; //Stop the loop, we don't need it anymore.
        }
    }
    return 1;
}
And, there is no difference as this function will be called along side again and again either if it's out the loop.
Reply
#6

There is not need to get the player vehicle ID as the callback already has it ..
Reply
#7

Quote:
Originally Posted by Cypress
Посмотреть сообщение
And, there is no difference as this function will be called along side again and again either if it's out the loop.
There is a difference because variables are faster than functions. But in this case you already have the function parameter so you might as well use it.
Reply
#8

ONEV is called when player press enter button so GetPlayerVehicleID didnt works? xd

If you are using static vehicles, simply way

pawn Код:
new CGLSVehicles[23]; // is from 0 to 22

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    if(CGLSVehicles[0] <= vehicleid <= CGLSVehicles[22])
    {
        // do something
    }
    return 1;
}
if not, use Cypress code
Reply
#9

Apologies, use 'vehicleid' as per my code snippet instead of GetPlayerVehicleID. I'm too used to working with OnPlayerStateChange!

Ash
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)