SA-MP Forums Archive
Problems with foreach in vehicles - 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: Problems with foreach in vehicles (/showthread.php?tid=534675)



Problems with foreach in vehicles - xXGangstaXx - 30.08.2014

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.


Re: Problems with foreach in vehicles - Pottus - 30.08.2014

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