How to insert fluid in an entity's fluidbox

Place to get help with not working mods / modding interface.
Post Reply
User avatar
IngoKnieto
Fast Inserter
Fast Inserter
Posts: 106
Joined: Mon Oct 03, 2016 9:29 am
Contact:

How to insert fluid in an entity's fluidbox

Post 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...

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

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

Post 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.

User avatar
IngoKnieto
Fast Inserter
Fast Inserter
Posts: 106
Joined: Mon Oct 03, 2016 9:29 am
Contact:

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

Post 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.

Post Reply

Return to “Modding help”