Cmd not working for new vehicles
#1

Hello, I made a command to open/close vehicle trunks. It works for vehicles that were spawned when the server first started up but any other vehicles spawned afterwards through for example /vspawner won't respond to the command. I tried using MAX_VEHICLES but that made no difference.

pawn Код:
CMD:trunk(playerid, params[]) //New vehicles won't work
{
    new engine,lights,alarm,doors,bonnet,boot,objective,Float:x,Float:y,Float:z;
    if(IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, -1, "You can't do this while inside a vehicle.");
    for(new i = 0; i < GetVehiclePoolSize(); i++)
    {
        GetVehiclePos(i, x, y, z);
        if(IsPlayerInRangeOfPoint(playerid, 4, x, y, z))
        {
            GetVehicleParamsEx(i, engine, lights, alarm, doors, bonnet, boot, objective);
            if(boot == VEHICLE_PARAMS_ON)
            {
                SetVehicleParamsEx(i, engine, lights, alarm, doors, bonnet, VEHICLE_PARAMS_OFF, objective);
                return 1;
            }
            else
            {
                SetVehicleParamsEx(i, engine, lights, alarm, doors, bonnet, VEHICLE_PARAMS_ON, objective);
                return 1;
            }
        }
    }
    return 1;
}
If anyone has any ideas on why this doesn't work, it would be appreciated.
Reply
#2

It's a loop problems.
PHP код:
CMD:trunk(playeridparams[]) //New vehicles won't work
{
    new 
engine,lights,alarm,doors,bonnet,boot,objective,Float:x,Float:y,Float:z;
    if(
IsPlayerInAnyVehicle(playerid)) return SendClientMessage(playerid, -1"You can't do this while inside a vehicle.");
    for(new 
GetVehiclePoolSize(); 0i--)
    {
        
GetVehiclePos(ixyz);
        if(
IsPlayerInRangeOfPoint(playerid4xyz)) // put your code bellow if you want; i modified it for test
        
{
            
GetVehicleParamsEx(ienginelightsalarmdoorsbonnetbootobjective);
            
SetVehicleParamsEx(ienginelightsalarmdoorsbonnetboot == VEHICLE_PARAMS_ON VEHICLE_PARAMS_OFF VEHICLE_PARAMS_ONobjective);
            return 
1;
        }
    }
    return 
1;

Reply
#3

Ohh I see thank you.

pawn Код:
//for(new i = 0; i < GetVehiclePoolSize(); i++)
for(new i = GetVehiclePoolSize(); i > 0; i--)
I did that and it works now. I was going to ask for you to explain but I understand now, the way I had the loop was once it reached the poolsize it stopped so it didn't run through the last vehicle. I just tested the old loop and it was basically the highest vehicle id was the one not being looped through. Cheers dude.
Reply
#4

Quote:
Originally Posted by Randouso
Посмотреть сообщение
pawn Код:
//for(new i = 0; i < GetVehiclePoolSize(); i++)
for(new i = GetVehiclePoolSize(); i > 0; i--)
It's because the first one had a missing = which made the code to loop till the loop ID is less than the vehicles in the server, which didn't loop the last one.

for(new i = 0; i <= GetVehiclePoolSize(); i++)
is the un-optimized loop, it calls the GetVehiclePoolSize everytime it loops/ iterate, while...
for(new i = 0, j = GetVehiclePoolSize(); i <= j; i++)
calls GetVehiclePoolSize once and saves its value to the variable.
But,
for(new i = GetVehiclePoolSize(); i > 0; i--)
Is slightly improved than the second one, because it uses one variable, and saves the value of the vehicles in the server and reduces the amount till the vehicle amount is more than 0. This code starts backward while the first two start from the smallest.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)