Page 1 of 1

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

Posted: Thu Jun 26, 2025 5:02 pm
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.

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

Posted: Fri Jun 27, 2025 6:16 pm
by Osmo
You could set entity.operable to false, and restore it to true on the next tick

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

Posted: Fri Jun 27, 2025 9:25 pm
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.

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

Posted: Sat Jun 28, 2025 5:03 am
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.

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

Posted: Sun Jun 29, 2025 12:48 am
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!