Problems with foreach in vehicles
#1

I got this code:

pawn Код:
//On top of script
new Iterator:Vehicle<2>;

//Both in OnGameModeInit
stock CreateVehicles()
{
    Iter_Add(Vehicle, AddStaticVehicle(541,164.6181,-54.6665,1.2031,269.3175,58,8)); //veh1
    Iter_Add(Vehicle, AddStaticVehicle(411,165.2549,-44.5700,1.3052,270.9745,64,1)); //veh2
    return 1;
}
stock AssignVehicleFuel()
{
    foreach (new vid : Vehicle)
    {
        new rand = randomEx(25,100);
        VInfo[vid][vFuel] = rand;
        printf("Vehicle ID: %d              |               Fuel: %d",vid, VInfo[vid][vFuel]);
    }
    return 1;
}

//After code
stock randomEx(min, max)
{
    new rand = random(max-min)+min;
    return rand;
}
Somehow it only prints vehicle 1's data, vehicle 2's data does not print.
It's weird because when i get in vehicle 2 it identifies it correctly.

Please help.

EDIT: I fixed it by increasing the size of
Quote:

new Iterator:Vehicle<2>;

by one more than the vehicles I created.
Reply
#2

This is not how Iterators should be used your size should be MAX_VEHICLES of course.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)