Dropping items/modules into machines interacts poorly with LuaSurface#create_entity

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Thremtopod
Burner Inserter
Burner Inserter
Posts: 9
Joined: Wed Nov 06, 2024 8:52 pm
Contact:

Dropping items/modules into machines interacts poorly with LuaSurface#create_entity

Post by Thremtopod »

I'm working on a custom module-type mod that (for implementation reasons) sometimes requires swapping out a crafting machine with a tweaked version of itself when one of the modded modules is added/removed from it. This can actually be achieved almost perfectly with the built in LuaSurface#create_entity function (kudos to the devs for having such attention to detail!):

View Code

There's only one issue I can't seem to solve. Usually when a player drops an item/module into a machine (i.e. by presses "Z"), only 1 item is added to the machine; but if that machine gets fast-replaced, because it's now a different entity, 1 more item gets dropped into it (the same effect as holding "Z" and dragging the cursor across multiple entities). Normally this behavior wouldn't be a problem... but it's an issue when combined with my modded modules. When I drop a single modded module into a machine, it triggers the mod's logic to fast-replace the entity—which causes another module to get dropped into the machine, triggering the fast-replace logic again—and so on until the machine's module slots fill up.

Note: I've only tested entities being fast-replaced by script; I've not tested entities being fast replaced by another player.

This may be too niche an issue to merit addressing. But if it's reasonable, it'd be nice if there were some way for the "don't drop another item into this entity yet" logic to be transferred over to the new entity.
Osmo
Long Handed Inserter
Long Handed Inserter
Posts: 81
Joined: Wed Oct 23, 2024 12:08 pm
Contact:

Re: Dropping items/modules into machines interacts poorly with LuaSurface#create_entity

Post by Osmo »

You could set entity.operable to false, and restore it to true on the next tick
Thremtopod
Burner Inserter
Burner Inserter
Posts: 9
Joined: Wed Nov 06, 2024 8:52 pm
Contact:

Re: Dropping items/modules into machines interacts poorly with LuaSurface#create_entity

Post by Thremtopod »

Osmo wrote: Fri Jun 27, 2025 6:16 pm You could set entity.operable to false, and restore it to true on the next tick
Unfortunately this doesn't work; the extra module is just getting dropped in during the next tick. At 60 ticks/s, the number of ticks during which the "Z" key is down from a single press is enough to fill any machine's module slots.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4015
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Dropping items/modules into machines interacts poorly with LuaSurface#create_entity

Post by boskid »

Dropping of items in this case is performed by entity selection having to select new entity on the client side. If there would be any chance at preventing this logic from tripping after entity swap is if you detect that an entity to be replaced was selected before a replace (LuaPlayer::selected), and by updating selection by script after entity was replaced.
Thremtopod
Burner Inserter
Burner Inserter
Posts: 9
Joined: Wed Nov 06, 2024 8:52 pm
Contact:

Re: Dropping items/modules into machines interacts poorly with LuaSurface#create_entity

Post by Thremtopod »

boskid wrote: Sat Jun 28, 2025 5:03 am ...detect that an entity to be replaced was selected before a replace (LuaPlayer::selected), and by updating selection by script after entity was replaced.
Wow, this works! I'm continually blown away by Factorio's mod support. Thanks!
Post Reply

Return to “Modding interface requests”