Looping through vehicles with Foreach
#1

Hi guys, can someone please tell me how I can go about looping through an enum (faction vehicles) so that I can create a command to reset all unoccupied faction vehicles?

pawn Код:
enum teamCars
{
    //police
    c01,
    c02,
    c03,
    c04,
    c05,
    c06,
    c07,
    c08,
    c09,
    c010,
    c011,
    c012,
    c013,
    c014,
    c015,
    c016,
    c017,
    c018,
    c019,
    c020
}
new Cars[teamCars];
Then of course I allocate the values to my vehicles in OnGameModeInit..

Then I was creating the foreach loop like:

pawn Код:
foreach(Cars[teamCars], i)
{
//code here
}
The above clearly isn't correct.

Can someone please provide a link to a tutorial they know of that is clear and easy to understand? I have been searching but the ones out there just don't seem to help me.

Also if you're going to provide code, I would much appreciate an explanation as to what the code does and why it has to be that way round (im here to learn)... if you simply can't be arsed to do this, I will try and learn the code from reading it (i can of course).

Thank you in advance!
Reply
#2

Why would you do that? Bad design and it is causing you problems already!

You should have something like

Код:
#define VEHICLE_TYPE_STANDARD 0
#define VEHICLE_TYPE_COP 1

enum VEHICLEINFO {
gVehicleType,
...
...
}

gVehicleData[MAX_VEHICLES][VEHICLEINFO];

foreach(new i : Vehicles)
if(gVehicleData[i][gVehicleType] == VEHICLE_TYPE_COP) {}
This will limit the need for multiple enums, iterators, variables.
Reply
#3

Quote:
Originally Posted by Pottus
Посмотреть сообщение
Why would you do that? Bad design and it is causing you problems already!

You should have something like

Код:
#define VEHICLE_TYPE_STANDARD 0
#define VEHICLE_TYPE_COP 1

enum VEHICLEINFO {
gVehicleType,
...
...
}

gVehicleData[MAX_VEHICLES][VEHICLEINFO];

foreach(new i : Vehicles)
if(gVehicleData[i][gVehicleType] == VEHICLE_TYPE_COP) {}
This will limit the need for multiple enums, iterators, variables.
Can you please tell me how I assign my vehicle a value that gets looped by? Sorry, I'm kinda new to the whole foreach thing.

Thanks!
Reply
#4

Look at here man everything you need to know.

https://sampforum.blast.hk/showthread.php?tid=571159
Reply
#5

Quote:
Originally Posted by Pottus
Посмотреть сообщение
Look at here man everything you need to know.

https://sampforum.blast.hk/showthread.php?tid=571159
That's exactly what I was following before and bam, error, error, error. No idea why. Thanks anyway.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)