Simply, I’m just trying to make a fuel system in the most optimised way possible. Since it has a chance of being hefty on memory and performance if not done efficiently.
I’ve thought of comparing two position values; current and previous to see how much fuel needs to be taken depending on the fuel consumption the vehicle may have.
Any other ideas?
I don’t see why it would be hefty on memory - it’s just a single number representing how much fuel the car has, right? In my personal experience, I wouldn’t bother optimising it intensely unless you determine it’s holding you back on doing something else or lowering performance significantly.
local Fuel = 100
-- Returns true if there is fuel, false if there isn't
local function ReduceFuel()
if Fuel > 0 then
Fuel = Fuel - 1
return true
else
return false
end
end
** bare bone example **
Then just call the ReduceFuel function when the cars velocity is > ~0.5 in a loop somewhere (I’m sure the vehicle system you’re using has a loop for adjust sound pitch). You should probably have some wait between the fuel reduction too otherwise it’ll empty too quickly
Would of thought putting it in a loop would be slightly memory intensive
Alright thank you
It’s not something you need to worry about, especially if it’s just subtracting a number. Large, complex operations, maybe - but this is very quick stuff.
Okay makes sense, thanks
This is a small example of fuel being consumed linearly over time.
local car = {}
car.__index = car
car.Capacity = 100
car.Consumption = 0.5
car.TimeSinceRefuel = 0
car.Started = 0
function car.new(capacity, consumption)
return setmetatable({On = false, Capacity = capacity, Consumption = consumption}, car)
end
function car:Refuel()
self:TurnOff()
self.TimeSinceRefuel = 0
self:TurnOn()
end
function car:TurnOn()
self.On = true
self.Started = tick()
end
function car:TurnOff()
self.On = false
self.TimeSinceRefuel = self.TimeSinceRefuel + tick() - self.Started
end
function car:GetFuel()
local on = self.On and 1 or 0
return math.clamp(self.Capacity - self.Consumption*(self.TimeSinceRefuel + on*(tick()-self.Started)), 0, self.Capacity)
end
return car
I typically have fancied doing a system where you need to be pressing the throttle for the fuel to be used (this way if they let off the throttle they can "coast)
I also check the velocity.magnitude of the vehicleseat, and had set “speeds” where they would lose more fuel or less fuel. Kinda as a “fuel saving” dynamic. But this was for racing, and probably more advanced than you’d need.
Then I had a brick that when hit, it would set the fuel back to 100.
Interesting concept though
Helpful, thanks