SA-MP Forums Archive
Adding Func. in less lines possible - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Adding Func. in less lines possible (/showthread.php?tid=456540)



Adding Func. in less lines possible - Stereotype - 06.08.2013

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;
}



Re: Adding Func. in less lines possible - Konstantinos - 06.08.2013

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



Re: Adding Func. in less lines possible - Stereotype - 06.08.2013

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


Re: Adding Func. in less lines possible - Psymetrix - 06.08.2013

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;
}



Re: Adding Func. in less lines possible - Misiur - 06.08.2013

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


Re: Adding Func. in less lines possible - Stereotype - 08.08.2013

Thanks guys , rep+ ..


Re: Adding Func. in less lines possible - Pottus - 08.08.2013

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


Re: Adding Func. in less lines possible - Stereotype - 09.08.2013

thanks