Foreach crashing the server, any idea why?
#1

So, I have an iterator to hold the server vehicles;

PHP код:
new Iterator:Vehicles<MAX_VEHICLES>;
vehicle_Create(modelFloat:xFloat:yFloat:zFloat:ac1c2delay)
{
    new 
vehicleid CreateVehicle(modelxyzac1c2delay);
    
    
Iter_Add(Vehiclesvehicleid);
    
    
printf("Added vehicleid %i to the iterator."vehicleid);
    
Vehicle(vehicleidModel) = model;
    
Vehicle(vehicleidFuel) = vehicle_ReturnFuelTank(vehicleid);
    
Vehicle(vehicleidColor1) = c1;
    
Vehicle(vehicleidColor2) = c2;
    return 
vehicleid;
}
vehicle_Destroy(vehicleid)
{
    
Iter_Remove(Vehiclesvehicleid);
    
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 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.
Reply
#2

Because you're removing an iterator while in a foreach loop, use Iter_SafeRemove instead
Reply
#3

https://sampforum.blast.hk/showthread.php?tid=637444
Reply
#4

There is already an existant iterator for vehicle like players
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)