[Include] Foreach Extended
#1

Foreach Extended


I recently had the need to optimize vehicle loops so I thought the best way to do that would be to use foreach so that I wouldn't loop through vehicles that didn't exist. I thought other developers might need to do this sometime so I figured I'd put together an include that creates an iterator to store valid vehicle IDs by hooking CreateVehicle, AddStaticVehicle, etc.

If people like this and wish for me to add iterators for things such as objects, pickups, etc to the include I'd be happy to do so but I don't have a need for it currently so it is only vehicles.

Functions

This include does not add any new functions but does add a new iterator called "Vehicle" for use with foreach or y_iterate.

Usage

You use this include as you would any other foreach loop.

pawn Код:
foreach(new v : Vehicle)
{
    ChangeVehicleColor(v, 123, 123);
}
Dependencies

Since this include is made for foreach it requires either foreach or y_iterate to be included before it.

Foreach

OR

YSI (Specifically y_iterate)

Credits

****** for foreach and y_iterate

Download

Github
Reply
#2

Pretty sure this will have issues if you loop and delete vehicles without using Iter_SafeRemove()
Reply
#3

Quote:
Originally Posted by Pottus
Посмотреть сообщение
Pretty sure this will have issues if you loop and delete vehicles without using Iter_SafeRemove()
Very nice point :P, other than that, this will work well, just make sure you add that so our vehicle id's dont get fucked up in the loops :P (And other bugs)...

Iter_SafeRemove(Vehicle, vehicleid); right?
Reply
#4

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Very nice point :P, other than that, this will work well, just make sure you add that so our vehicle id's dont get fucked up in the loops :P (And other bugs)...
He can also just return -1 to indicate that the vehicleid didn't exist in that case you could just continue iterating without having to set the next iteration in the loop.

pawn Код:
foreach(new i : Vehicle)
{
    new next;    
    next = DestroyVehicle(vehicleid);
    if(next > -1) i = next;
}
pawn Код:
stock ForeachExt_DestroyVehicle(vehicleid)
{
    new success = DestroyVehicle(vehicleid);
    if(success) Iter_SafeRemove(Vehicle, vehicleid, success);
        else success = -1;
    return success;
}
Reply
#5

But replacing Iter_Remove(Vehicle, vehicleid); with a safe remove cleans up just as well right? Let me check...

btw:
pawn Код:
stock ForeachExt_DestroyVehicle(vehicleid)
{
    new success = DestroyVehicle(vehicleid);
    if(success)
    {
        Iter_Remove(Vehicle, vehicleid);
    }
    return success;
}
So if, the success is false, or 0, return -1? Why?

Oh I see I think, same concept
Reply
#6

Ohh I totally didn't think about filterscripts, I'll fix that right away thanks! I'll make DestroyVehicles work with loops as well. Also, the only reason I have DestroyVehicle return a value is to keep it consistent in the fact it returns 1 if successful and 0 if not.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)