Adding Func. in less lines possible
#1

Hello .. I wanna do Attach3dTextLabelToVehicle but in less lines possible .. problem is there are about 10 cars and do i need to make functions for all vehicles separetely or i can make it in few lines..

I already made public function for detecting this cars

pawn Код:
public IsAnsipaCar(carid)
{
    if((carid >= 23) && (carid <= 35))
    {
        return 1;
    }
    return 0;
}
Reply
#2

pawn Код:
public IsAnsipaCar(carid)
{
    switch(carid)
    {
        case 23 .. 35: return 1;
        // another example:
        //case 45, 47, 78, 96, 105 .. 109: return 1;
    }
    return 0;
}
Reply
#3

I don't need that .. ... I need to Attach3dTextLabelToVehicle to that vehicles in less lines posible
Reply
#4

Don't use vehicle IDs directly. Create a new array to hold the vehicle IDs then check if the vehicle ID is in the array.

pawn Код:
// Top of script
// Array sizes MUST match
new gAnsipaVehicle[3]; // Holds 3 vehicles (0 - 2)
new Text3D:gAnsipaVehicleLabel[3]; // Holds 3 labels(0 - 2)

// OnGameModeInit/OnFilterScriptInit
gAnsipaVehicle[0] = AddStaticVehicle(...);
gAnsipaVehicle[1] = AddStaticVehicle(...);
gAnsipaVehicle[2] = AddStaticVehicle(...);

// loop through and attach a label to all vehicle IDs stored in the array
for (new i; i < sizeof(gAnsipaVehicle); i++)
{
    gAnsipaVehicleLabel[i] = Create3DTextLabel(...);
    Attach3DTextLabelToVehicle(gAnsipaVehicleLabel[i], gAnsipaVehicle[i]);
}

// New IsAnsipaCar function
bool:IsAnsipaVehicle(vehicleid)
{
    for (new i; i < sizeof(gAnsipaVehicle); i++)
    {
        if (vehicleid == gAnsipaVehicle[i])
        {
            return true;
        }
    }
    false;
}
Reply
#5

Implement solution from @Psymetrix post. But if you choose not to, your function can look like
pawn Код:
public IsAnsipaCar(carid)
{
    return 23 <= carid <= 35;
}
#e: Sorry, looks like my post is irrevelant to question - tired I guess
Reply
#6

Thanks guys , rep+ ..
Reply
#7

Psymetrix method is not very good either and completely sidesteps the issue of not having your own scripted create and delete vehicle. If you did this then you would be adding vehicles like this....


pawn Код:
#define VEHICLE_TYPE_SOMETHING 1

enum VINFO
{
   vID,
   vType
}

new VehicleData[MAX_VEHICLES][VINFO] = { INVALID_VEHICLE_ID, ... };

AddVehicle(vehicletype, Float:x, Float:y, Float:z, Float:fa, color1, color2, respawntime)
{
    new index = CreateVehicle(........);
    VehicleData[index][vID] = index;
    VehicleData[index][vType] = VEHICLE_TYPE_SOMETHING;
}

IsASomething(vehicleid)
{
    if(VehicleData[vehicleid][vType] == VEHICLE_TYPE_SOMETHING) return 1;
    return 0;
}
This should give you a better idea the vehicleid becomes the array reference in VehicleData, this way you can see what the vehicle type is without doing any looping, creating extra arrays etc. In fact you can do the following with this kind of setup

- add many different kinds of vehicles types
- remove any vehicle
- define more data members in the enum to keep track of fuel, position etc
Reply
#8

thanks
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)