Read fluid in assembling machine input/output slot [control.lua][SOLVED]

Place to get help with not working mods / modding interface.
Post Reply
User avatar
oldskoolmatt
Inserter
Inserter
Posts: 21
Joined: Wed Nov 11, 2020 10:23 am
Contact:

Read fluid in assembling machine input/output slot [control.lua][SOLVED]

Post by oldskoolmatt »

Pretty straight forward, it was fairly easy to read contents for items using this bit of code, yet, i can't get it to work with fluids, what am i missing?

Code: Select all

local count = player.surface.count_entities_filtered{area=DEFINED_AREA, type="assembling-machine"}
if count >=  1 then
    for _, entity in pairs(player.surface.find_entities_filtered{area=DEFINED_AREA, type="assembling-machine"}) do

        local item_input = {}
        local item_output = {}
				
        for content, amount in pairs(entity.get_inventory(defines.inventory.assembling_machine_output).get_contents()) do
            item_output = MY_FUNCTION(content, amount)
        end
		
        for content, amount in pairs(entity.get_inventory(defines.inventory.assembling_machine_input).get_contents()) do
            item_input =  MY_FUNCTION(content, amount)
        end
    end
end
Last edited by oldskoolmatt on Thu Jan 20, 2022 1:34 pm, edited 1 time in total.
Image

User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 318
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: Read fluid in assembling machine input/output slot [control.lua]

Post by Stringweasel »

If you want the input/output fluid of an assembly machine I'm pretty sure you should use the fluid boxes and not the inventory.

https://lua-api.factorio.com/latest/Lua ... y.fluidbox
Alt-F4 Author | Factorio Modder
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Read fluid in assembling machine input/output slot [control.lua]

Post by Pi-C »

oldskoolmatt wrote:
Thu Jan 20, 2022 11:53 am
Pretty straight forward, it was fairly easy to read contents for items using this bit of code, yet, i can't get it to work with fluids, what am i missing?
Have you ever seen an unbarreled fluid in any inventory? Try get_fluid_contents() instead!

EDIT: entity.get_fluid_contents will return all fluids in an entity, so if you have fluids as input and as output, querying the output fluidbox directly (as suggested by stringweasel) would be better.

By the way:

Code: Select all

local count = player.surface.count_entities_filtered{area=DEFINED_AREA, type="assembling-machine"}
if count >=  1 then
    for _, entity in pairs(player.surface.find_entities_filtered{area=DEFINED_AREA, type="assembling-machine"}) do
…
    end
end
Do you really need the count? Otherwise, surface.find_entities_filtered will always return a table (which may be empty), so the wrapper around the for-loop shouldn't be necessary.
Last edited by Pi-C on Thu Jan 20, 2022 1:19 pm, edited 1 time in total.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
oldskoolmatt
Inserter
Inserter
Posts: 21
Joined: Wed Nov 11, 2020 10:23 am
Contact:

Re: Read fluid in assembling machine input/output slot [control.lua]

Post by oldskoolmatt »

Do you really need the count? Otherwise, surface.find_entities_filtered will always return a table (which may be empty), so the wrapper around the for-loop shouldn't be necessary.
The script keeps scanning for entities (on_tick) and apparently COUNT is much more UPS friendly than FIND.
Runnning a COUNT check first and then, only if the count check is met, running the FIND command, helps preventing UPS drops when many different entities are around and no wanted entities are nearby
Image

User avatar
oldskoolmatt
Inserter
Inserter
Posts: 21
Joined: Wed Nov 11, 2020 10:23 am
Contact:

Re: Read fluid in assembling machine input/output slot [control.lua]

Post by oldskoolmatt »

Stringweasel wrote:
Thu Jan 20, 2022 1:05 pm
If you want the input/output fluid of an assembly machine I'm pretty sure you should use the fluid boxes and not the inventory.

https://lua-api.factorio.com/latest/Lua ... y.fluidbox
Indeed, i did try early on but i did write it wrong, the correct syntax is as follow

Code: Select all

local fluid = entity.fluidbox[1]
fluid_input = MY_FUNCTION(fluid.name, fluid.amount)
probably need to add some safety checks like fluidbox_valid etc etc
Last edited by oldskoolmatt on Thu Jan 20, 2022 2:29 pm, edited 1 time in total.
Image

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Read fluid in assembling machine input/output slot [control.lua]

Post by Pi-C »

oldskoolmatt wrote:
Thu Jan 20, 2022 1:30 pm
The script keeps scanning for entities (on_tick) and apparently COUNT is much more UPS friendly than FIND.
Agreed, that makes sense. But from your code snippet it wasn't clear this would run in on_tick. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
oldskoolmatt
Inserter
Inserter
Posts: 21
Joined: Wed Nov 11, 2020 10:23 am
Contact:

Re: Read fluid in assembling machine input/output slot [control.lua]

Post by oldskoolmatt »

This is not serviceable in this exact case (on_tick script), it will definitely crash at some point
Indeed, i did try early on but i did write it wrong, the correct syntax is as follow

Code: Select all

local fluid = entity.fluidbox[1]
fluid_input = MY_FUNCTION(fluid.name, fluid.amount)
probably need to add some safety checks like fluidbox_valid etc etc
Turns out that this syntax is the way to go:

Code: Select all

if entity and entity.fluidbox then
    for content, amount in pairs(entity.get_fluid_contents()) do
        fluid_input = MY_FUNCTION(content, amount)
    end
end
Image

Post Reply

Return to “Modding help”