SA-MP Forums Archive
Loop through all vehicles. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Loop through all vehicles. (/showthread.php?tid=452414)



Loop through all vehicles. - arjanforgames - 21.07.2013

How can I loop through all vehicles?
If I know that I can set there engine state OnVehicleSpawn to OFF.
Thanks.


Respuesta: Loop through all vehicles. - Xabi - 21.07.2013

pawn Код:
for(new c = 0; c < MAX_VEHICLES; c++)



Re: Respuesta: Loop through all vehicles. - IstuntmanI - 21.07.2013

Quote:
Originally Posted by Xabi
Посмотреть сообщение
pawn Код:
for(new c = 0; c < MAX_VEHICLES; c++)
The correct one is
pawn Код:
for( new c = 1; c <= MAX_VEHICLES; c ++ )
Because vehicle IDs start from 1 and finish at 2000.


Re: Loop through all vehicles. - arjanforgames - 21.07.2013

I now have this but it doesnt work:

pawn Код:
public OnVehicleSpawn(vehicleid)
{
    for( new c = 1; c <= MAX_VEHICLES; c ++ )
    {
        new engine, lights, alarm, doors, bonnet, boot, objective;
        GetVehicleParamsEx(c, engine, lights, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(c,0,lights,alarm,doors,bonnet,boot,objective);
    }
    return 1;
}



Re: Loop through all vehicles. - Misiur - 21.07.2013

Ouch. You are looping through all vehicles when any new vehicle spawns. Why not something like
pawn Код:
public OnVehicleSpawn(vehicleid)
{
    new engine, lights, alarm, doors, bonnet, boot, objective;
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
    SetVehicleParamsEx(vehicleid, 0, lights,alarm,doors,bonnet,boot,objective);
    return 1;
}



Re: Loop through all vehicles. - DobbysGamertag - 21.07.2013

Quote:
Originally Posted by arjanforgames
Посмотреть сообщение
I now have this but it doesnt work:

pawn Код:
public OnVehicleSpawn(vehicleid)
{
    for( new c = 1; c <= MAX_VEHICLES; c ++ )
    {
        new engine, lights, alarm, doors, bonnet, boot, objective;
        GetVehicleParamsEx(c, engine, lights, alarm, doors, bonnet, boot, objective);
        SetVehicleParamsEx(c,0,lights,alarm,doors,bonnet,boot,objective);
    }
    return 1;
}
That looks to me like the cars will go into a huge loop.

1st car spawns turn off its engine
2nd car spawns turn off its engine and the 1st cars engine.
3rd car spawns turn off its engine and the 2nd and 1st car engine.

Why dont you set the parameters like:

pawn Код:
public OnVehicleSpawn(vehicleid)
{
    new engine, lights, alarm, doors, bonnet, boot, objective;
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
    SetVehicleParamsEx(vehicleid, 0,lights,alarm,doors,bonnet,boot,objective);
    return 1;
}



Re: Loop through all vehicles. - arjanforgames - 21.07.2013

I now have:

pawn Код:
public OnVehicleSpawn(vehicleid)
{
    new engine, lights, alarm, doors, bonnet, boot, objective;
    GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
    SetVehicleParamsEx(vehicleid, 0,lights,alarm,doors,bonnet,boot,objective);
    return 1;
}

But whenever I use the command /car the engine is already running;

pawn Код:
CMD:car(playerid, params[])
{
    new Float:x, Float:y, Float:z, Float:az;
    new vehid;
    GetPlayerPos(playerid, x, y, z);
    GetPlayerFacingAngle(playerid, az);
    vehid = CreateVehicle(402, x+1, y+5, z, az, -1, -1, 180);
    PutPlayerInVehicle(playerid, vehid, 0);
    return 1;
}



Re: Loop through all vehicles. - IstuntmanI - 21.07.2013

OnVehicleSpawn is actually OnVehicleRespawn, you have to use SetVehicleParamsEx after you create each vehicle.


Re: Loop through all vehicles. - arjanforgames - 21.07.2013

So how would I apply this to vehicles that are made with AddStaticVehicle? (BTW is AddStaticVehicle the best way to add vehicles?)


Re: Loop through all vehicles. - zDevon - 21.07.2013

Using ManualVehicleEngineAndLights(); (https://sampwiki.blast.hk/wiki/ManualVehicleEngineAndLights) in OnGameModeInit will accomplish this for you.