Page 1 of 1

How to insert fluid in an entity's fluidbox

Posted: Fri Dec 22, 2017 12:46 pm
by IngoKnieto
I am trying to insert a fluid in an empty fluidbox of an entity, but it's not working like I think...
For example if I want to fill the storage tank with 1000 water when it gets placed, I would do the following:

Code: Select all

function fill_storagetank(tank)
  tank.fluidbox[1] = {type="water", amount=1000}
end

--function is called on_built_entity, that part works
script.on_event(defines.events.on_built_entity, function(event)
 if event.created_entity.name == "storage-tank" then
    fill_storagetank(event.created_entity)	
  end
end)
However that is not working, it throws the error: bad argument #-1 to '__newindex' (string expected, got nil)
I am thinking that probably the fluidbox doesn't exist, because the tank is empty. Is that possible? But how do I create it?
If anyone could point me in the right direction here, I would appreciate it...

Re: How to insert fluid in an entity's fluidbox

Posted: Fri Dec 22, 2017 1:24 pm
by Optera
You have to instanciate the fluidbox to change it's properties as described in the api
http://lua-api.factorio.com/latest/LuaFluidBox.html

Code: Select all

function fill_storagetank(tank)
  fluid =  tank.fluidbox[1]
  fluid.type= "water"
  fluid.amount = 1000
  tank.fluidbox[1] = fluid
end
Also note that with 0.16 type was changed to name.

Re: How to insert fluid in an entity's fluidbox

Posted: Fri Dec 22, 2017 2:27 pm
by IngoKnieto
Optera wrote:Also note that with 0.16 type was changed to name.
Thank you, that was the cause of this - I am testing with version 0.16.7.
Now it works:

Code: Select all

function fill_storagetank(tank)
  tank.fluidbox[1] = {name="water", amount=1000}
end


The instanciation you mentioned is not even necessary, I suppose you just need to do that if you want to modify an existing fluid.