Issue regarding to the fuel.
#1

Hey everyone. I am having this issue regarding to the speedometer's fuel thing. Basically, when I am in vehicle and turned the engine off, the fuel is still lowering and sometimes when I drive, the fuel textdraw does not update, it is like freezed. It updates only when I exit and enter the car again and then it shows it's real value, but then suddenly it goes back to 100. Can you help me please to fix it?


Here is some code:

OnGameModeInIt
PHP код:
SetTimer("Speedometer"100true);
     for(new 
i=0;i<MAX_VEHICLES;i++)
        {
        
fuel[i] = 100;
        }
     
SetTimer("timer_fuel_lower",20000,true); 
PHP код:
public OnPlayerStateChange(playeridnewstateoldstate)
{
    if (
newstate == PLAYER_STATE_DRIVER || newstate == PLAYER_STATE_PASSENGER)
    {
        new 
vid GetPlayerVehicleID(playerid);
        new 
string[125];format(string,sizeof string,"Fuel: ~y~%i~w~ L",fuel[vid]);
        
TextDrawSetString(td_fuel[playerid],string);
        
TextDrawShowForPlayer(playerid,Textdraw2[playerid]);
        
TextDrawShowForPlayer(playerid,Textdraw1[playerid]);
        
TextDrawShowForPlayer(playerid,speedbox[playerid]);
        
TextDrawShowForPlayer(playerid,HEALTH[playerid]);
        
TextDrawShowForPlayer(playerid,td_fuel[playerid]);
    }     else {
        
TextDrawHideForPlayer(playerid,td_fuel[playerid]);
        
TextDrawHideForPlayer(playerid,Textdraw2[playerid]);
        
TextDrawHideForPlayer(playerid,Textdraw1[playerid]);
        
TextDrawHideForPlayer(playerid,HEALTH[playerid]);
        
TextDrawHideForPlayer(playerid,speedbox[playerid]);
        } 
PHP код:
public timer_fuel_lower()
{
    for(new 
i=0;i<MAX_PLAYERS;i++) {
        new 
vid GetPlayerVehicleID(i);
        if (
GetPlayerVehicleSeat(i) == 0) {
            
fuel[vid] = fuel[vid] -1;
            if (
fuel[vid]<1)
            {
                
fuel[vid] = 0;
                
SetVehicleParamsEx(vid,0,0,0,0,1,1,0); //change this to watever you like
                
GameTextForPlayer(i,"~r~You are out of ~w~fuel~r~!",5000,4); //show text
            
}
        }
        new 
string[125];format(string,sizeof string,"Fuel: ~y~%i~w~ L",fuel[vid]);
        
TextDrawSetString(td_fuel[i],string);
    }
    return 
1;

+ I was using https://sampforum.blast.hk/showthread.php?tid=169284 to create a fuel system for a speedo.
Reply
#2

In timer_fuel_lower, use GetVehicleParamsEx function and check if "engine" is 0 or -1*. If it is, then don't lower the fuel as the engine of the vehicle is off.

* It is returned -1 when a parameter (such as engine) has not been set. I'm not entirely sure if ManualVehicleEngineAndLights has not been used and it hasn't been set to any value, whether the engine is off or not (you'll need to test it to make sure).

Some things to change:

- When declaring "fuel" array, set a default value.

pawn Код:
new fuel[MAX_VEHICLES] = {100, ...};
instead of using a loop when the gamemode starts (2000 iterations) to set a value:
pawn Код:
for(new i=0;i<MAX_VEHICLES;i++)
{
    fuel[i] = 100;
}
- Avoid declaring variables inside loops.
- Use foreach/y_iterate for player-loops. If not, at least GetPlayerPoolSize.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)