Page 1 of 1
insert_fluid with fluid_box filter check for created entity [control.lua][SOLVED]
Posted: Tue Sep 21, 2021 12:25 am
by oldskoolmatt
What i am trying to achieve is to check if the fluid_box.filter [data.lua] value is water, then insert 10 water
Code: Select all
if event.created_entity.type == "boiler" and event.created_entity.fluid_box.filter == "water" then
event.created_entity.insert_fluid({name = "water", amount = 10})
end
script.on_event(defines.events.on_built_entity, on_built_entity)
script.on_event(defines.events.on_robot_built_entity, on_built_entity)
event.created_entity.fluid_box.filter == "water" doesn't work at all, what am i missing?
Re: insert_fluid with fluid_box filter check for created entity [control.lua]
Posted: Tue Sep 21, 2021 1:16 am
by Silari
https://lua-api.factorio.com/latest/LuaFluidBox.html
I don't see a 'filter' property on there at all - it's get_filter[index] to read a filter at the given index. The return is a FluidBoxFilter where 'name' would be what you'd check is == 'water'
Re: insert_fluid with fluid_box filter check for created entity [control.lua]
Posted: Tue Sep 21, 2021 9:11 am
by oldskoolmatt
I'm actually having a hard time in figuring out the correct syntax, whatever i do it returns nil, i'm quite familiar with the data stage, while control stage tends to become a pain in the arse to me
Re: insert_fluid with fluid_box filter check for created entity [control.lua]
Posted: Tue Sep 21, 2021 8:11 pm
by Silari
oldskoolmatt wrote: Tue Sep 21, 2021 9:11 am
I'm actually having a hard time in figuring out the correct syntax, whatever i do it returns nil, i'm quite familiar with the data stage, while control stage tends to become a pain in the arse to me
Just remember that there are quite a few differences in how things are laid out, especially with names, and that
https://lua-api.factorio.com/latest/ is where you should be looking for things - NOT the wiki, as the Prototypes pages there are only for the data stage.
Are you trying it on boilers that are empty? From checking it myself, the fluidboxes are nil unless there is currently something in it.
Looking at it, you might be needing to check the entity's prototype, where it has
FluidBoxPrototypes with a .filter property that can tell you what fluids it accepts. If that filter is what you need then you can edit the actual fluidbox to have the fluid added.
Re: insert_fluid with fluid_box filter check for created entity [control.lua]
Posted: Sat Sep 25, 2021 2:12 am
by oldskoolmatt
That's exactly what i am trying to do, and yes, in the prototype the filter is defined as follow
Code: Select all
data.raw["boiler"][BOILER_NAME].fluid_box.filter = STRING
Though i'm still not sure on how to hook the property from control stage and what the right syntax would be
Re: insert_fluid with fluid_box filter check for created entity [control.lua]
Posted: Sat Sep 25, 2021 3:38 am
by Silari
entity.prototype has a reference to the
LUAEntityPrototype for the entity - the names aren't entirely the same as in the data stage but generally pretty close. From there you can loop over the fluidbox_prototypes property looking for the one where .filter.name == 'water'
Can't run the game right now to check code or anything, but a very simple solution would I believe look like
Code: Select all
if event.created_entity.type == "boiler" and event.created_entity.prototype.fluidbox_prototypes[1].filter.name == "water" then
event.created_entity.insert_fluid({name = "water", amount = 10})
end
Note that assumes the first fluid box is the input, and there is only one input (which seemed to be the case for vanilla boilers when I was checking the other day, but may not always be the case with mods), and that there is only place water could go.
Using a loop to go over them would be better - you'd probably want to make sure that the production_type on the fluidbox_prototype is "input" or "input-output", then use the .index property of the fluidbox_prototype to see what fluidbox on the entity it corresponds to, then adding the water to the fluidbox directly. Again, probably not a problem using the above code with the vanilla boilers, and maybe not any modded ones, but it might.
Re: insert_fluid with fluid_box filter check for created entity [control.lua]
Posted: Sun Sep 26, 2021 10:29 pm
by oldskoolmatt
Silari wrote: Sat Sep 25, 2021 3:38 am
Code: Select all
if event.created_entity.type == "boiler" and event.created_entity.prototype.fluidbox_prototypes[1].filter.name == "water" then
event.created_entity.insert_fluid({name = "water", amount = 10})
end
WORKED LIKE A CHARM! Thanks for taking the time to help me solve this, i definitely learned quite a bit on how prototypes are handled during control stage
EDIT: Here is the updated code in a for loop, it works with any (modded) boiler
Code: Select all
if event.created_entity.type == "boiler" then
for _, fluidbox in pairs(event.created_entity.prototype.fluidbox_prototypes) do
local filter = fluidbox.filter
local production_type = fluidbox.production_type
if (filter and filter.name == "water") and (production_type and production_type == "input-output") then
event.created_entity.insert_fluid({name = "water", amount = 10})
end
end
end