[Help] LuaFluidbox does not contain key 'amount'

Place to get help with not working mods / modding interface.
Boolet
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Aug 26, 2017 9:44 pm
Contact:

[Help] LuaFluidbox does not contain key 'amount'

Post by Boolet »

I'm trying to make a function that looks at the fluidbox of each pump on each tick and modifies the amount of water stored in each one. However, every time I try to access my_pump_array.fluidbox.amount I get a 'no such key' error, even when my_pump_array ~= nil returns true and my_pump_array.fluidbox ~= nil returns true.

Am I missing something about how fluidboxes work? Do I have to wait until there is some amount of fluid in the fluidbox before I can look up amount? I've tested this from so many angles and I just can't figure out why I'm getting an error. :x :cry:

This is the most recent test code I was using to try and figure this out.
local pumps = {}
pumps = game.surfaces["nauvis"].find_entities_filtered({type = "offshore-pump"})
game.players[1].print(#pumps)
for i=1,#pumps do
if pumps.fluidbox ~= nil then
game.players[1].print(pumps.fluidbox.amount)
end
end


Any help would be appreciated! Thanks!!!
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: [Help] LuaFluidbox does not contain key 'amount'

Post by Nexela »

fluidbox returns an array of fluidboxes

Code: Select all


for _, pump in pairs(game.surfaces["nauvis"].find_entities_filtered({type = "offshore-pump"})) do
  for i =1, #pump.fluidbox do
    local box = pump.fluidbox[i]
      if box then
       game.players[1].print(box.amount)
    end
  end
end

Do note that reading from a LuaFluidBox creates a new table and writing will copy the given fields from the table into the engine's own fluid box structure. Therefore, the correct way to update a fluidbox of an entity is to read it first, modify the table, then write the modified table back. Directly accessing the returned table's attributes won't have the desired effect.

--edit Added safety checks
Boolet
Burner Inserter
Burner Inserter
Posts: 5
Joined: Sat Aug 26, 2017 9:44 pm
Contact:

Re: [Help] LuaFluidbox does not contain key 'amount'

Post by Boolet »

Thanks a bunch. I had seen that note on the documentation but I guess I didn't really appreciate its implications.
Post Reply

Return to “Modding help”