foreach Vehicle iterator
#1

Hello, I am wondering how I would go about creating an accurate iterator for vehicles, this is my current idea:
- I am trying to make one 100% alike the Iterator:Player (When the vehicle is removed with DestroyVehicle the iter is removed with Iter_Remove from the variable Vehicle, and when a vehicle is created the iter is added with Iter_Add)
My question is, will this work?

pawn Code:
new Iterator:Vehicle<MAX_VEHICLES>;

public OnVehicleSpawn(vehicleid)
{
    Iter_Add(Vehicle, vehicleid);
    return 1;
}

public OnVehicleDeath(vehicleid)
{
    Iter_Remove(Vehicle, vehicleid);
    return 1;
}

// in a function
getNumVehicles()
{
    return Iter_Count(Vehicle); // will return the amount of vehicles spawned, not just the first id to be invalid.
}
Reply
#2

No.

1. OnVehicleSpawn is only called when a vehicle REspawns; it is not called when the vehicle is created. For this you'd have to hook AddStaticVehicle, AddStaticVehicleEx and CreateVehicle.

2. OnVehicleDeath does not mean the vehicle is destroyed. The vehicle will respawn shortly after.

3. You need to hook DestroyVehicle also.

4. Your function to count vehicles is not needed as foreach has Iter_Count().
Reply
#3

@MP2, thanks, but how would I get the vehicleid for Iter_Add/Remove in AddStaticVehicle, AddStaticVehicleEx and CreateVehicle?

Also, if the number is already in the iter, Vehicle, then Iter_Add will do nothing, and if the number isn't in the iter, then Iter_Remove will do nothing:
Quote:

Iter_Add(name, value)

Adds a value to the named iterator. If the value is invalid or already present it is simply ignored.

Iter_Remove(name, value)

Removes a value from the named iterator. If the value is invalid or not present it is simply ignored.

Edit: Changed the function to use Iter_Count()
Reply
#4

http://pastebin.com/VLUEQ70B
Reply
#5

Thanks for the help.
Reply
#6

Also, you can use Itter_Contains() to check if a vehicle exists. Most people just get the model though, and if it's 0 the vehicle doesn't exist. It's probably more efficient.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)