Fuel system help please.
#1

I made a small fuel system, but when player exists the vehicle the fuel stays the same when he went out,
how can I make like that the fuel will keep going down.

pawn Код:
public CheckGas()
{
    foreach(Player, i)
    {
        if(GetPlayerState(i) == PLAYER_STATE_DRIVER)
        {
            new vehicle = GetPlayerVehicleID(i);
            if(IsADMVCar(vehicle) || !engineOn[vehicle]) { return 1; }
            if(engineOn[vehicle])
            {
                Gas[vehicle]--;
                if(Gas[vehicle] == 0)
                {
                    VehicleEngine(vehicle, false);
                }
            }
        }
    }
    return 1;
}
Reply
#2

Loop through all your vehicles (not players, because unmanned vehicles can't be found that way), check if its engine is running, and decrease fuel status.
Reply
#3

pawn Код:
public CheckGas()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(IsADMVCar(i) || !engineOn[i]) { return 1; }
        if(engineOn[i])
        {
            Gas[i]--;
            if(Gas[i] == 0)
            {
                VehicleEngine(i, false);
            }
        }
    }
    return 1;
}
It works without if(IsADMVCar(i) || !engineOn[i]) { return 1; }
and with if(IsADMVCar(i) || !engineOn[i]) { return 1; }
it's not.
Reply
#4

Don't you need to return 0 when an engine isn't running instead of 1?
Reply
#5

It still the same
Reply
#6

In fact, return is wrong in this place, use continue instead to skip iteration

pawn Код:
public CheckGas()
{
    for(new i = 0; i < MAX_VEHICLES; i++)
    {
        if(IsADMVCar(i) || !engineOn[i]) continue;
        Gas[i]--;
        if(Gas[i] == 0) VehicleEngine(i, false);
    }
    return 1;
}
Reply
#7

Thank you man very man!
so, continue it's just to end the loop?
Reply
#8

Not exactly. https://sampwiki.blast.hk/wiki/Control_Structures#break & https://sampwiki.blast.hk/wiki/Control_Structures#continue
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)