Page 1 of 1
LuaEntity::inserters (or some such to get all inserters and/or loaders targeting this entity)
Posted: Sun Oct 12, 2025 4:15 am
by protocol_1903
As in title, I would like a way to get all of the inserters and/or loaders that target a specific entity, mainly inserters. It is already possible to do this by checking in an area around the entity, then checking if the pickup_target or drop_target match that entity. That's intensive, though, and rather hard to calculate a search area since mods can make inserters really long. I believe that the engine already has access to this information, or at least can do a lot more optimized search pattern than a mod could do in lua.
Re: LuaEntity::inserters (or some such to get all inserters and/or loaders targeting this entity)
Posted: Sun Oct 12, 2025 4:19 am
by PennyJim
Considering my understanding is that an entity "wakes up" the inserters around it, I would assume this information is already available in some form.
Don't know how convertible to an api field that is though
Re: LuaEntity::inserters (or some such to get all inserters and/or loaders targeting this entity)
Posted: Sun Oct 12, 2025 4:25 am
by protocol_1903
I assume it wouldn't be too difficult to wrap into a function that returns a table of LuaEntities
Re: LuaEntity::inserters (or some such to get all inserters and/or loaders targeting this entity)
Posted: Sun Oct 12, 2025 5:40 am
by boskid
Closest data available would be a list of all entities for which LuaEntity::drop_target returns that specific entity, effectively a reverse lookup. This data is obtainable.
My main concern here is that modders may start reporting such reverse lookup as innacurate if they would create for example an inserter and expect that it will be immediately visible through such read when this would not be the case: inserter only looks for the drop target during update, so when it is not updated since entity was just created, or because it is unable to update (for example because entity is marked to be deconstructed or because it is a ghost) it would not be visible through this reverse lookup. Other entities would appear when different conditions are met (mining drill looks for drop target when it wants to drop an item, or when it is rotated, crafter will look for drop target during update when there are items in the output inventory, or when it is rotated). If this would be expected to work instantly (when script creates one entity, second entity would report it as being able to feed items) or to also work with ghosts, then this feature would be rejected just because this query would require to visit every entity in game because inserters with custom drop vector can have them set to be literally anything (at least when changing drop vectors at runtime).
Re: LuaEntity::inserters (or some such to get all inserters and/or loaders targeting this entity)
Posted: Sun Oct 12, 2025 5:48 am
by protocol_1903
Is the issue that inserters would not be immediately visible, or that entities that eventually are a target don't know this so they have no visible reverse lookups, or both?
I would be fine with just-created inserters not being valid reverse lookup targets, my main focus is having just-created entities having valid reverse lookup targets
Re: LuaEntity::inserters (or some such to get all inserters and/or loaders targeting this entity)
Posted: Sun Oct 12, 2025 5:53 am
by boskid
A "just created" entity would report empty list of entities in the reverse lookup just because other entities were not updated yet and so this new entity would not be a drop target of any entity yet. You can verify this right now by creating an entity and then iterating over all entities on the surface and checking which of then are reporting your entity through LuaEntity::drop_target read - there will be a delay of when this is updated (if this is updated at all).