Foreach crashing the server, any idea why? - 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: Foreach crashing the server, any idea why? (
/showthread.php?tid=637842)
Foreach crashing the server, any idea why? -
Sjn - 22.07.2017
So, I have an iterator to hold the server vehicles;
PHP код:
new Iterator:Vehicles<MAX_VEHICLES>;
vehicle_Create(model, Float:x, Float:y, Float:z, Float:a, c1, c2, delay)
{
new vehicleid = CreateVehicle(model, x, y, z, a, c1, c2, delay);
Iter_Add(Vehicles, vehicleid);
printf("Added vehicleid %i to the iterator.", vehicleid);
Vehicle(vehicleid, Model) = model;
Vehicle(vehicleid, Fuel) = vehicle_ReturnFuelTank(vehicleid);
Vehicle(vehicleid, Color1) = c1;
Vehicle(vehicleid, Color2) = c2;
return vehicleid;
}
vehicle_Destroy(vehicleid)
{
Iter_Remove(Vehicles, vehicleid);
DestroyVehicle(vehicleid);
printf("Removed vehicleid %i from the iterator.", vehicleid);
}
#if defined _ALS_DestroyVehicle
#undef DestroyVehicle
#else
#define _ALS_DestroyVehicle
#endif
#define DestroyVehicle vehicle_Destroy
This works, however, when I call the function to destroy the vehicle inside a loop like;
PHP код:
foreach(new i : Vehicles)
{
// Few if statements on this line to check for specific vehicles to delete.
{
DestroyVehicle(i);
}
}
It's crashing the server. When I use DestroyVehicle for a specific vehicle id, it works fine tho. The printf inside the DestroyVehicle function does not print out anything when it crashes the server nor there are any crash log, server just freezes at it is.
Can anyone help me point out the issue? I am using the standalone version of foraech.
Re: Foreach crashing the server, any idea why? -
Eoussama - 22.07.2017
Because you're removing an iterator while in a foreach loop, use
Iter_SafeRemove instead
Re: Foreach crashing the server, any idea why? -
Paulice - 22.07.2017
https://sampforum.blast.hk/showthread.php?tid=637444
Re: Foreach crashing the server, any idea why? -
Dayrion - 22.07.2017
There is already an existant iterator for vehicle like players