[Ayuda] foreach ...
#9

foreach no funciona solo para jugadores. Es un bucle mediante iteradores, por eso es mucho mas eficiente que cualquier bucle a travez de los jugadores, pero pueden agregarse iteradores personalizados y utilizarlos.
Esto puede realizarse mediante la funcion:
pawn Код:
Iter_Add(name, value);
Donde name, representa el nombre del Iterador al cual se le asignara el elemento llamado value.

Ejemplo:
pawn Код:
//Declaracion de nuestro nuevo iterador:
new Iterator:Vehicles<MAX_VEHICLES>;

//hookeamos un par de funciones para el correcto uso del mismo
stock _AddStaticVehicle(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2)
{
    new veh = AddStaticVehicle(modelid, spawn_x, spawn_y, spawn_z, angle, color1, color2)
    if(veh != INVALID_VEHICLE_ID)
        Iter_Add(Vehicles, veh);
    return veh;
}
#define AddStaticVehicle    _AddStaticVehicle

stock _AddStaticVehicleEx(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:angle, color1, color2, respawn_delay)
{
    new veh = AddStaticVehicle(modelid, spawn_x, spawn_y, spawn_z, angle, color1, color2, respawn_delay)
    if(veh != INVALID_VEHICLE_ID)
        Iter_Add(Vehicles, veh);
    return veh;
}
#define AddStaticVehicleEx    _AddStaticVehicleEx

stock _CreateVehicle(modelid, Float:x, Float:y, Float:z, Float:angle, color1, color2, respawn_delay)
{
    new veh = CreateVehicle(modelid, x, y, z, angle, color1, color2, respawn_delay);
    if(veh != INVALID_VEHICLE_ID)
        Iter_Add(Vehicles, veh);
    return veh;
}
#define CreateVehicle    _CreateVehicle

stock _DestroyVehicle(vehicleid)
{
    if(DestroyVehicle(vehicleid))
    {
        Iter_Remove(Vehicles, vehicleid);
        return 1;
    }
    return 0;
}
#define DestroyVehicle    _DestroyVehicle

//1є posible uso:
new Float:heath;
foreach(new i : Vehicles)
{
    GetVehicleHealth(i, health);
    printf("La vida del vehiculo %i es: %.3f", i, health);
}

//2є posible uso:
printf("Hay %i vehiculos creados", Iter_Count(Vehicles));
Para mas informacion sobre foreach lean el topico de su lanzamiento, que comprenderan mucho mejor su gran capacidad.

Nota: Para que el script ejemplo que deje, funcione correctamente, deberian agregarse las funciones en cada script y se deberia definir el iterador de forma tal que el mismo sea comun a todos los scripts (Deberia editarse el include para utilizar CallRemoteFunction y acceder a una funcion declarada en el GM para acceder al iterador correspondiente, algo no muy sensillo).
Reply


Messages In This Thread
[Ayuda] foreach ... - by OTACON - 04.10.2012, 00:43
Respuesta: [Ayuda] foreach ... - by CaptainMactavish - 04.10.2012, 01:57
Respuesta: [Ayuda] foreach ... - by OTACON - 04.10.2012, 02:11
Respuesta: [Ayuda] foreach ... - by Fluid016 - 04.10.2012, 12:51
Respuesta: [Ayuda] foreach ... - by xSDx - 04.10.2012, 15:20
Re: Respuesta: [Ayuda] foreach ... - by CaptainMactavish - 04.10.2012, 15:43
Respuesta: Re: Respuesta: [Ayuda] foreach ... - by xSDx - 04.10.2012, 15:44
Re: [Ayuda] foreach ... - by digman - 04.10.2012, 17:04
Respuesta: [Ayuda] foreach ... - by TheChaoz - 04.10.2012, 18:12
Respuesta: Re: Respuesta: [Ayuda] foreach ... - by Fluid016 - 05.10.2012, 00:54

Forum Jump:


Users browsing this thread: 2 Guest(s)