Fuel System Textdraw
#1

I created a textdraw for fuel system. Everything is working fine for one player. But after driving the second vehicle, the previous value of fuel is overlapping with new one and both the values are decreasing together.

PHP код:
new fueltimer[MAX_VEHICLES]; 
PHP код:
public OnPlayerConnect(playerid)
{
    
VehicleMeter[playerid] = TextDrawCreate(498.000000140.000000" ");
    
TextDrawBackgroundColor(VehicleMeter[playerid], 255);
    
TextDrawFont(VehicleMeter[playerid], 1);
    
TextDrawLetterSize(VehicleMeter[playerid], 0.3899991.299999);
    
TextDrawColor(VehicleMeter[playerid], 0xFFFFFFFF);
    
TextDrawSetOutline(VehicleMeter[playerid], 1);
    
TextDrawSetProportional(VehicleMeter[playerid], 1);
    
    return 
1;

PHP код:
public OnPlayerStateChange(playeridnewstateoldstate)
{
    new 
vid GetPlayerVehicleID(playerid);
    if(
newstate == PLAYER_STATE_DRIVER)
    {
        
TextDrawShowForPlayer(playeridVehicleMeter[playerid]);
        
fueltimer[vid] = SetTimerEx("DecreaseFuel"10001"ii"playeridvid);
    }
    else
    {
        
TextDrawHideForPlayer(playeridVehicleMeter[playerid]);
        
KillTimer(fueltimer[vid]);
    }
    return 
1;

PHP код:
public OnPlayerExitVehicle(playeridvehicleid)
{
    
KillTimer(fueltimer[vehicleid]);
    
TextDrawHideForPlayer(playeridVehicleMeter[playerid]);
    return 
1;

Function:
PHP код:
public DecreaseFuel(playeridvehicleid// Decreases fuel by 0.035 (Whole fuel finishes in almost 50 minutes)
{
    new 
enginelightsalarmdoorsbonnetbootobjectivestring[128];
    
GetVehicleParamsEx(vehicleidenginelightsalarmdoorsbonnetbootobjective);
    if(
IsBicycle(vehicleid) == || IsValidDealershipVehicle(vehicleid) == || engine == 0)
        return 
1;
    else
    {
        if(
Vehicle[vehicleid][vFuel] <= 0)
        {
            
ToggleEngine(vehicleidVEHICLE_PARAMS_OFF);
            
SendClientMessage(playeridCOLOR_LIGHTRED"Your vehicle is out of fuel.");
            
KillTimer(fueltimer[vehicleid]);
        }
        
format(stringsizeof(string), "Fuel: %d%%"floatround(Vehicle[vehicleid][vFuel]));
        
TextDrawSetString(VehicleMeter[playerid], string);
        
Vehicle[vehicleid][vFuel] -= 0.035;
    }
    return 
1;

Reply
#2

Anyone?
Reply
#3

That's why you should use player textdraws.. Also destroy them onplayerexitvehicle instead of hiding them. Create the textdraw again for the new vehicle onplayerentervehicle. You should also restore all the values you use to calculate when the player enters a vehicle.
Reply
#4

PHP код:
public OnPlayerEnterVehicle(playeridvehicleidispassenger)
{
    
fueltimer[vehicleid] = SetTimerEx("DecreaseFuel"10001"ii"playeridvehicleid);
    
VehicleMeter[playerid] = CreatePlayerTextDraw(playerid498.000000140.000000" ");
    
PlayerTextDrawBackgroundColor(playeridVehicleMeter[playerid], 255);
    
PlayerTextDrawFont(playeridVehicleMeter[playerid], 1);
    
PlayerTextDrawLetterSize(playeridVehicleMeter[playerid], 0.3899991.299999);
    
PlayerTextDrawColor(playeridVehicleMeter[playerid], 0xFFFFFFFF);
    
PlayerTextDrawSetOutline(playeridVehicleMeter[playerid], 1);
    
PlayerTextDrawSetProportional(playeridVehicleMeter[playerid], 1);
    return 
1;

PHP код:
public OnPlayerExitVehicle(playeridvehicleid)
{
    
KillTimer(fueltimer[vehicleid]);
    
PlayerTextDrawDestroy(playeridVehicleMeter[playerid]);
    return 
1;

It's still giving the same problems.
Reply
#5

wtf are you even doing, to you understand?
when you write code, first and most important thing is to understand what this code is doing.
Reply
#6

Quote:
Originally Posted by jasperschellekens
Посмотреть сообщение
That's why you should use player textdraws.. Also destroy them onplayerexitvehicle instead of hiding them. Create the textdraw again for the new vehicle onplayerentervehicle. You should also restore all the values you use to calculate when the player enters a vehicle.
Unless you have a lot of player textdraws it makes no sense to delete them and even if you did have a lot it is difficult to approach the limit. In certain cases where TD's would be generated from iterated loops yeah I could see that method being beneficial. However your suggestion makes absolutely no sense in this case use the right technique for the job!
Reply
#7

Quote:
Originally Posted by CodeStyle175
Посмотреть сообщение
wtf are you even doing, to you understand?
when you write code, first and most important thing is to understand what this code is doing.
That is the problem. Everything that is using timers on my server is doing like this. Everything works well for the first player. For the second player, everything runs twice. For the third, thrice and so on. WTH is going on?
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)