PDA

View Full Version : Foreach with iterator


GospodinX
22/08/2018, 12:56 PM
Hi guys,

I'm begin to work with iterator's,so I think that i'm doing something wrong.I use iterator for player's vehicle.(List of player's vehicles) I have problem with foreach.

new Iterator: Vehicles[MAX_PLAYERS]<MAX_CAR>;


OnPlayerDisconnect (There I destroy vehicles..)
foreach(new i: Vehicles[playerid])
{
DestroyVehicle(VehicleInfo[i][vID]);
printf("%d %d",i, playerid);
}

Server_log: http://prntscr.com/klmj0d (So my server spam, i = 0 and i don't know why he don't want to get out from loop(Foreach).I think it spam when player don't have any vehicle.
For now,I'm do this:

new number = Iter_Count(Vehicles[playerid]);
if(number > 0)
{
foreach...

It work's now.But i'm not sure do I need to check player vehicles before foreach loop?

GospodinX
24/08/2018, 09:28 AM
bump...

None1337
24/08/2018, 11:10 AM
Did you add Iter_Init(Vehicles); in OnGameModeInit?

Also, you can use this:

foreach(new i: Vehicles[playerid])
{
printf("%d %d",i, playerid);
printf("[debug] destroying vehicle %i", VehicleInfo[i][vID]);
DestroyVehicle(VehicleInfo[i][vID]);
Iter_SafeRemove(Vehicles[playerid], i, i);
}

GospodinX
24/08/2018, 11:35 AM
No.I don't use Iter_Init anytime in my gamemode.I need to add this for every iterator which I use on OnGameModeInit?

I use this when player log in:
Iter_Add(Vehicles[playerid], id_vozila);
But just if player have vehicles.

GospodinX
24/08/2018, 05:32 PM
Again, if you’re using the latest version of the include you should get a warning along the lines of symbol is never used "Iter_Init@Vehicles".


Now I'm download latest version but I'm not getting any warning :)
I will add it,thank you!