Jump to content

[Help] Refuelling, rendering causing problems.


Mann56

Recommended Posts

Hey guys,

I was making a fuel system and while making a typical refuel station, i encountered this problem, when i press space to refuel, it increments the no every time it renders ...

How can i prevent this from happening?

Here is the client code :

  
sx_ , sy_ = guiGetScreenSize () 
sx, sy = sx_/1366 , sy_/768  
fuel = 100000 
fuelprice = 3 
vehfuel = 100 
totvehfuel = 150 
state = "true" 
fueltype = "petrol" 
totfuel = 1000 
  
function refuelTheVeh(cmd,dn,veh1) 
fuel = getElementData(veh1,"fuel") 
         setElementData(veh1,"fuel",fuel+1) 
         outputChatBox(""..fuel.."",255,255,0) 
         end 
  
-- now lets draw the dxGui when a marker is hit 
function drawMarkerGUI ( thePlayer ) 
dxDrawRectangle(sx*420,sy*230,sx*470,sy*280,tocolor(0,0,0,150)) 
dxDrawText ( "Fuel Station" , sx*560, sy*235, sx*490, sy*235, tocolor ( 255, 255, 0, 255 ), 1, "bankgothic" ) 
dxDrawText("Fuel Type : " .. fueltype .. "", sx*510,sy*290,sx*500,sy*240,tocolor(255,255,255,255),1 ,"bankgothic") 
dxDrawText("Price : $" .. fuelprice .. "", sx*510,sy*330,sx*500,sy*240,tocolor(255,255,255,255),1 ,"bankgothic") 
dxDrawText("Total Fuel : " ..totfuel.." litres", sx*470,sy*370,sx*500,sy*240,tocolor(255,255,255,255),1 ,"bankgothic") 
dxDrawText("Your Fuel : " ..vehfuel.."/"..totvehfuel.. " litres", sx*440,sy*410,sx*500,sy*240,tocolor(255,255,255,255),1 ,"bankgothic") 
local veh = isPedInVehicle( localPlayer ) 
if veh then 
     engstate = true 
     veh1 = getPedOccupiedVehicle (localPlayer) 
     getEngineState = getVehicleEngineState(veh1) 
end 
if getEngineState == engstate then 
         dxDrawText("Please switch off the engine", sx*435,sy*450,sx*500,sy*240,tocolor(255,255,0,255),0.95 ,"bankgothic") 
elseif getEngineState ~= engstate then 
         setElementData(veh1,"fuel",100) 
         dxDrawText("Press SPACE to refuel!", sx*475,sy*450,sx*500,sy*240,tocolor(255,255,0,255),0.95 ,"bankgothic") 
         bindKey("space","up",refuelTheVeh,veh1) 
            
end  
end  

Link to comment
setElementData(veh1,"fuel",100) 

Why are you setting the 'fuel' data in onClientRender instead of refuelTheVeh function? Also, you're binding the key each over and over again.

What you could do is check if the player is pressing space inside onClientRender and refill the fuel. You could also use getTickCount to slow it down.

Link to comment

I set the fuel data inside the render function as i want it to function well before i input fuel, so whenever it refuels it starts from 100.

What you could do is check if the player is pressing space inside onClientRender and refill the fuel. You could also use getTickCount to slow it down.

I tried the first thing, but when i trigger the function to refuel when the key is pressed, it increments the fuel by the times it renders.

The second thing, i think it will be good but how can i slow it down?

Link to comment
elseif getEngineState ~= engstate then 
         setElementData(veh1,"fuel",100) 
         dxDrawText("Press SPACE to refuel!", sx*475,sy*450,sx*500,sy*240,tocolor(255,255,0,255),0.95 ,"bankgothic") 
         if (getKeyState('space')) then 
          if (not startTick) then startTick = getTickCount() end 
          if (getTickCount - startTick > 1000) then 
           --increment 
           startTick = getTickCount() 
           --if refilled then 
              -- startTick = nil 
           --end 
         end 
end 

Link to comment

A good rule of thumb is to never write gameplay mechanics inside of a renderer event unless absolutely necessary for a smooth transition. Keep in mind this is triggered with every frame, so you'll have it triggered as quickly as somewhere around every 16ms.

Using a timer would be far more efficient, or using tick like JR10 demonstrated. :)

Link to comment
A good rule of thumb is to never write gameplay mechanics inside of a renderer event unless absolutely necessary for a smooth transition. Keep in mind this is triggered with every frame, so you'll have it triggered as quickly as somewhere around every 16ms.

Using a timer would be far more efficient, or using tick like JR10 demonstrated. :)

Thanks for the idea of using timers, but JR10's method is also good :P And yeah, using any function or event in renders , it causes problems as the event/function gets triggered every time it renders the screen. When i used to refuel fuel from 100, it would increment upto 144 times in a single space click.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...