Community Forum

Fueltank level

Forum Overview >> Scripting

CategoryScripting
Created22.02.2021 00:49


Jack Arrow (JackArrow) 22.02.2021 00:49
Hey everyone,
i want to get the fueltank level of my current vehicle. Someone knows, how to get it?

This is a cutout of my first initension, but i got value 'nil' for spec.
In Giants Documentation i found out that the fuel tank level is used in the Specialization: Motorized/getCanMotorRun...so i tried to reach it:


function Fuel:onUpdate(dt)
if self:getIsActive() then
local spec = g_specializationManager:getSpecializationByName(motorized)
if spec.consumersByFillTypeName.diesel ~= nil then
local fuelLevel = self:getFillUnitFillLevelPercentage(spec.consumersByFillTypeName.diesel.fillUnitIndex)
end
print("Fuel: spec: "..tostring(spec)
print("Fuel: FuelLevel: "..tostring(fuelLevel))
end
end

Bilbo Beutlin (BBeutlin) 22.02.2021 02:37
The function 'getSpecializationByName()' gives the global specialization, not the vehicle's internal table.
Also, "motorized" must be then in quotes. It is a string, not a value. A typical rookie mistake. Do not confuse a[i] with a["i"], that's not the same.

Anyway, you must write

local spec = self.spec_motorized
if spec ~= nil then -- check it if unsure about 'motorized'

For the rest you can use the code as in Motorized:getCanMotorRun()

Jack Arrow (JackArrow) 22.02.2021 09:37
Tanks for your quick response.
--local spec = self.spec_motorized-- works well, but now theres a new problem. I allways get nil for local fuelLevel. This is actually my :onUpdate :

function Fuel:onUpdate(dt)
if self:getIsActive() then
local spec = self.spec_motorized
if spec ~= nil then
if spec.consumersByFillTypeName.diesel ~= nil then
local fuelLevel = self:getFillUnitFillLevel(spec.consumersByFillTypeName.diesel.fillUnitIndex)
end
print("Fuel: FuelLevel: "..tostring(fuelLevel))
end
for k,v in pairs(spec.consumersByFillTypeName.diesel) do
print(k)
print(v)
end
print("Fuel: spec.consumersByFillTypeName.diesel.fillUnitIndex: "..tostring(spec.consumersByFillTypeName.diesel.fillUnitIndex))
end
end

I tested:
local spec is as expected a table
spec.consumersByFillTypeName.diesel i printed with for k,v in pairs(...) do get a few data with falues -fine
spec.consumersByFillTypeName.diesel.fillUnitIndex gets 1 as we ve seen above allready

But fuelLevel is nil.
I dont understand why its not working because i copyed it from Motorized:getCanMotorRun() of Giants Documentation.
Another one: What is ment by self. Does it aim to the current data of the actuall activ vehicle data, something like currentMission - a giant table where is everything in it?

Im new to lua, sry for my greenness :)


Bilbo Beutlin (BBeutlin) 22.02.2021 12:54
I've no idea why it doesn't work at yours in opposite to default LUA code.
Perhaps check and print-out step-by-step each variable part to see where you get nil.

And yes, for the specializations the "self" construct points to the root table of the vehicle (usually).
So might also try another method by scanning the 'self.spec_fillUnit' table.

Jack Arrow (JackArrow) 22.02.2021 15:19
I got it.

Dont know why but converting with tonumber solved my problem -.-

local fuelLevel = self:getFillUnitFillLevel(tonumber(spec.consumersByFillTypeName.diesel.fillUnitIndex))

Thanks for your support :)


Note: Log in to post. Create a new account here.