SA-MP Forums Archive
Fuel decreases even though velocity is 0 - 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: Fuel decreases even though velocity is 0 (/showthread.php?tid=633390)



Fuel decreases even though velocity is 0 - NealPeteros - 30.04.2017

Topic explains everything. This timer is called every 6 seconds

PHP код:
public timer_update()
{
    for(new 
i=0;i<MAX_PLAYERS;i++)
    {
        new 
Float:Velocity[3];
        new 
vid GetPlayerVehicleID(i);
          
GetVehicleVelocity(vidVelocity[0], Velocity[1], Velocity[2]);
        if(
Velocity[0] == && Velocity[1] == && Velocity[2] == 0) return 0;
        if (
isrefuelling[i]) return 0;
        if (
GetPlayerVehicleSeat(i) == 0)
        {
            if(
Engine[i] == 1)
            {
                
fuel[vid] = fuel[vid] - 1;
                if (
fuel[vid]<1)
                {
                    
fuel[vid] = 0;
                    new 
veh GetPlayerVehicleID(i);
                    new 
engine,lights,alarm,doors,bonnet,boot,objective;
                    
GetVehicleParamsEx(veh,engine,lights,alarm,doors,bonnet,boot,objective);
                    
SetVehicleParamsEx(veh,VEHICLE_PARAMS_OFF,lights,alarm,doors,bonnet,boot,objective);
                    
GameTextForPlayer(i,"~r~You are out of ~w~fuel~r~!",5000,4);
                    
Engine[i] = 0;
                }
            }
        }
        new 
string[128];
        
format(string,sizeof string,"~b~Fuel: ~w~%i%"fuel[vid]);
        
PlayerTextDrawSetString(ivehFuel[i],string);
    }
    return 
1;




Re: Fuel decreases even though velocity is 0 - SoLetsGO - 30.04.2017

I am not a pawn expert but maybe you shouldn't compare Floats with Integers (e.g. Velocity[0] == 0).
Try to change this to Velocity[0] == 0.0.


Re: Fuel decreases even though velocity is 0 - Vince - 30.04.2017

You shouldn't compare floating point values with the equality operator anyway. Floating point values are not exact due the way they work and rounding errors may occur. For example, 3.6 will be read back as 3.5999999046325684. This is fine for most purposes because you don't need this exact precision but when comparing them they are not the same.

You should try printing the individual values of the velocity and see what they actually contain.


Re: Fuel decreases even though velocity is 0 - raydx - 30.04.2017

This timer will stop (crash) when someone is not in vehicle. You are not checking if vehicle id is valid before using vid variable. Also, you are returning 0 in loop - use continue; instead.