How to check if an int is in an array
#1

How to check if an int is in an array
For example:

Код:
new lspdinf[2],lspdche[4],lspdbul[4];

public OnGamemodeInit()
{
      	lspdinf[0] = AddStaticVehicleEx(411,1528.7384000,-1683.8818400,5.6906000,270.0000000,-1,-1,1600); 
	lspdinf[1] = AddStaticVehicleEx(411,1528.7384000,-1688.0616500,5.6906000,270.0000000,-1,-1,1600); 
	lspdche[0] = AddStaticVehicleEx(415,1544.5936300,-1663.1635700,5.7401000,90.0000000,-1,-1,1600); 
	lspdche[1] = AddStaticVehicleEx(415,1544.5927700,-1659.0278300,5.7401000,90.0000000,-1,-1,1600); 
	lspdche[2] = AddStaticVehicleEx(415,1544.5927700,-1655.0069600,5.7401000,90.0000000,-1,-1,1600); 
	lspdche[3] = AddStaticVehicleEx(415,1544.5927700,-1651.2161900,5.7401000,90.0000000,-1,-1,1600); 
	lspdbul[0] = AddStaticVehicleEx(541,1538.7780800,-1645.0030500,5.8306000,180.0000000,-1,-1,1600); 
	lspdbul[1] = AddStaticVehicleEx(541,1534.6420900,-1645.0029300,5.8306000,180.0000000,-1,-1,1600); 
	lspdbul[2] = AddStaticVehicleEx(541,1530.6212200,-1645.0029300,5.8306000,180.0000000,-1,-1,1600); 
	lspdbul[3] = AddStaticVehicleEx(541,1526.6007100,-1645.0029300,5.8306000,180.0000000,-1,-1,1600); 
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
        if(vehicleid == anyintfrom lspdinf[] || vehicleid == anyintfrom lspdche[] ...) // you get what i mean
        { don't let him in unless cop...}
}
Reply
#2

You need to loop through those array and check if ID exists in them.
PHP код:
//the function to check if its cop car or not
stock IsCopCar(vid)
{
         new 
i;
         for(
0sizeof(lspdinf); i++)if(vid == lspdinf[i])return true;
         for(
0sizeof(lspdche); i++)if(vid == lspdche[i])return true;
         for(
0sizeof(lspdbul); i++)if(vid == lspdbul[i])return true;
         return 
false;
}
//function usage: 
public OnPlayerEnterVehicle(playeridvehicleidispassenger)
{
        if(
IsCopCar(vehicleid))
        { 
dont let him in unless cop...}

Reply
#3

or
pawn Код:
new vehs[2];

public OnGamemodeInit()
{
    vehs[0] = AddStaticVehicleEx(411,1528.7384000,-1683.8818400,5.6906000,270.0000000,-1,-1,1600);
    AddStaticVehicleEx(411,1528.7384000,-1688.0616500,5.6906000,270.0000000,-1,-1,1600);
    AddStaticVehicleEx(415,1544.5936300,-1663.1635700,5.7401000,90.0000000,-1,-1,1600);
    AddStaticVehicleEx(415,1544.5927700,-1659.0278300,5.7401000,90.0000000,-1,-1,1600);
    AddStaticVehicleEx(415,1544.5927700,-1655.0069600,5.7401000,90.0000000,-1,-1,1600);
    AddStaticVehicleEx(415,1544.5927700,-1651.2161900,5.7401000,90.0000000,-1,-1,1600);
    AddStaticVehicleEx(541,1538.7780800,-1645.0030500,5.8306000,180.0000000,-1,-1,1600);
    AddStaticVehicleEx(541,1534.6420900,-1645.0029300,5.8306000,180.0000000,-1,-1,1600);
    AddStaticVehicleEx(541,1530.6212200,-1645.0029300,5.8306000,180.0000000,-1,-1,1600);
    vehs[1] = AddStaticVehicleEx(541,1526.6007100,-1645.0029300,5.8306000,180.0000000,-1,-1,1600);
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
        if((vehs[0] <= vehicleid <= vehs[1])) // you get what i mean
        { don't let him in unless cop...}
}
Reply
#4

I don't understand why people create vehicles like this it is absolutely counter intuitive of how it SHOULD be done quick example to make everything easier.

Код:
#define         VEHICLE_TYPE_NONE       0
#define         VEHICLE_TYPE_NORMAL     1
#define         VEHICLE_TYPE_COPCAR     2

enum VEHICLEINFO
{
	vType,
	Float:vSpawnHP,
}

static VehicleData[MAX_VEHICLES][VEHICLEINFO];
stock CreateFactionVehicle(type, modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay, Float:health=1000.0, addsiren=0)
{
	new index = CreateVehicle(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay, addsiren);
    VehicleData[index][vType] = type;
    VehicleData[index][vSpawnHP] = health;
	return index;
}

IsCopCar(vehicleid)
{
	if(vehicleid > 0 && vehicleid < MAX_VEHICLES)
		if(VehicleData[index][vType] == VEHICLE_TYPE_COPCAR)
		    return 1;
	return 0;
}

GetVehicleType(vehicle)
{
    if(vehicleid > 0 && vehicleid < MAX_VEHICLES)
		return VehicleData[vehicleid][vType];
	return VEHICLE_TYPE_NONE;
}
@Jeff what you are doing is extremely silly trying to use vehicleid ranges to determine vehicle types just blows my mind of the sheer illogics of such design.
Reply
#5

Thanks.
Reply
#6

@Pottus Thanks man, I see more sense and potential in your system, I myself didn't think about that possibility.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)