copy-paste by blueprint should trigger an event

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
mami
Burner Inserter
Burner Inserter
Posts: 18
Joined: Fri Oct 21, 2022 12:27 am
Contact:

copy-paste by blueprint should trigger an event

Post by mami »

I would like blueprinting over an existing entity to trigger the "defines.event.on_entity_settings_pasted" event https://lua-api.factorio.com/latest/eve ... ngs_pasted or otherwise it's own unique event. So for example, if I have two constant combinators with different settings, and I make a blueprint of one, and then I place the blueprint overtop the other, I've copy-pasted the settings from one combinator to the other. This operates the same as if I had used the entity copy-paste feature by shift-clicking, yet there is no event associated with it.

In this forum post on modding help we discuss how there isn't really a way to catch copy-paste by blueprint easily:
viewtopic.php?f=25&t=102702
The best method currently is to poll all of the relevant entities which isn't particularly UPS friendly.
Bizarrely enough

Code: Select all

entity.allow_copy_paste = false
does prevent copy-paste by blueprint as well as normal copy-paste. This inconsistency makes me guess this is just an oversight and I hope a way to catch copy-paste by blueprint can be added.

Ideally the event lets us see what the settings of the LuaEntity are before and after pasting, but just having an event with the LuaEntity attached to it at all will open up a lot of previously UPS expensive possibilities.
My specific use case is in the creation of a "logistics-combinator" similar to a logistics-train-stop from the Logistics Train Network Mod. It needs to be registered on one of a series of global table based on its current settings for my logistics algorithm to work correctly. There is no work around that I can think of that does not either remove features from my mod, involve polling all logistics-combinators periodically, or cause unexpected and glitchy behavior.
BinarySpike
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Jun 28, 2016 2:15 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by BinarySpike »

I'm making a mod with signals that are treated as variables in a blueprint. Once the player updates the variables, they get resolved on the entities placed by the blueprint.

If the player uses "shift" and places the blueprint, there is currently no easy way to detect what buildings were updated.

My solution is, in `defines.events.on_pre_build` to call `surface.find_entities(area?)` on each blueprint entity's position—translated into real-world coordinate. Then, check them a tick later to see if they have my custom variables in them. Unfortunately, I'm still struggling with how to convert blueprint coordinates into real-world coordinates.

A `defines.events.on_entity_settings_pasted` for each entity would work easily enough as I'm tracking bot, player, and revive builds between blueprint placement and GUI confirmation.

Ultimately, I'd love to see a `defines.events.on_blueprint_placed` that has properties like `placed_entities`, `updated_entities`, and possibly even `failed_entities`. (*edit not likely to happen per-Rseding91 in multiple posts)

* Edit
Related/Similar posts:
ILLISIS
Inserter
Inserter
Posts: 27
Joined: Wed Feb 09, 2022 10:49 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by ILLISIS »

+1 for defines.events.on_blueprint_placed significant benefits could be gained using this in Constructron-Continued instead of using on_built_entity and processing every entity!

I would need a way to see the area and position of the blueprint and required items to place the entities. I do not need positions of the entities within the blueprint however.
BinarySpike
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Jun 28, 2016 2:15 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by BinarySpike »

Would benefit this mod as well:
Image
SWeini
Inserter
Inserter
Posts: 39
Joined: Mon Apr 04, 2022 6:43 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by SWeini »

+1 for any kind of event after pasting over an existing entity (blueprint or copy&paste)
Also don't forget to include item tag data when pasting.

I'm playing around with an idea that involves adding tags to the blueprint entity when copying a specific building. Upon placement the item tags are moved to the ghost and upon ghost revival my script can act according to the tag data.

Unfortunately there is no way to react to the tag data when pasting over existing buildings.
BlackOverlord
Manual Inserter
Manual Inserter
Posts: 3
Joined: Fri Aug 20, 2021 5:00 pm
Contact:

Re: copy-paste by blueprint should trigger an event

Post by BlackOverlord »

+1
I have some settings associated with entities which I store in the global table and in blueprint tags. Unfortunately, blueprint pasting is not working as there is no event fired when it happens.

Would be great to have such an event so mods could properly support regaular game features.
GlassBricks
Inserter
Inserter
Posts: 43
Joined: Fri Jun 11, 2021 5:20 pm
Contact:

Re: copy-paste by blueprint should trigger an event

Post by GlassBricks »

+1
y_e_a_r
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed Jun 07, 2023 10:38 pm
Contact:

Re: copy-paste by blueprint should trigger an event

Post by y_e_a_r »

+1 to this, but would be even better to have a good solution to adding metadata to entities that plays nicely with blueprints.
User avatar
BraveCaperCat
Filter Inserter
Filter Inserter
Posts: 412
Joined: Mon Jan 15, 2024 10:10 pm
Contact:

Re: copy-paste by blueprint should trigger an event

Post by BraveCaperCat »

+1
Creator of multiple mods, including Quality Assurance - My most popular one.
Go check them out with the first and second links!
I'll probably be wanting or giving help with modding most of the time I spend here on the forum.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14942
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by Rseding91 »

I would add this, but due to how blueprints are built I can't send Lua events in the middle of placing the blueprint because mods may invalidate the blueprint which will crash the game.

If anyone ever figures out a way to make it work without mods being able to crash the game then we'll add it.
If you want to get ahold of me I'm almost always on Discord.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14942
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by Rseding91 »

Additionally: I can't fire the normal "pre-pasted" and "settings_pasted" events because I can't push the blueprint entity as the source in those events. The entity is not in the map (it exists just in the blueprint) and would not be valid to have a LuaEntity reference to.

So if I did manage to make this work, it would be a new event and would only contain "this entity had its settings pasted while blueprint building". There would be no "pre" event because again, I can't guarantee a mod wouldn't mess with the blueprint during the event and thus mess with the blueprint entity that will be pasting the settings from.

Would that even be useful?
If you want to get ahold of me I'm almost always on Discord.
robot256
Smart Inserter
Smart Inserter
Posts: 1068
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by robot256 »

Rseding91 wrote: Fri Jan 03, 2025 3:14 pm So if I did manage to make this work, it would be a new event and would only contain "this entity had its settings pasted while blueprint building" - would that even be useful?
Yes, this would be extremely useful. For example, the Shortwave mod uses a dummy constant combinator to hold the channel selection. This makes it easy to store in blueprints. The mod only changes the script wires when events trigger, indicating the value in the dummy combinator changed. The new event would allow triggering when a blueprint modified the channels of an existing entity.

I'm aware that if I were to rewrite thos particular mod, I would probably use a custom GUI. storage table, and blueprint tags to store channel selection. That brings up another question for blueprints -- would I be able to tell that the blueprint included a tag on an entity that it pasted over top of? Is the solution to look at the blueprint record data itself and infer which parts overlap with already-built entities?
curiosity
Filter Inserter
Filter Inserter
Posts: 579
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: copy-paste by blueprint should trigger an event

Post by curiosity »

Rseding91 wrote: Fri Jan 03, 2025 3:14 pm Would that even be useful?
Right now there isn't any indication that something happened at all. Anything will be useful at this point. Even if the event is not immediate, it would still be better than nothing.

If you can't pass a LuaEntity, pass what you can. Especially things that get lost in the process, like the aforementioned entity tags. Or maybe make a new LuaEntityOutsideMap type that allows for limited (even if just read-only) access (recipe, control behavior, other entity settings).
User avatar
hgschmie
Long Handed Inserter
Long Handed Inserter
Posts: 91
Joined: Tue Feb 06, 2024 5:18 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by hgschmie »

Rseding91 wrote: Fri Jan 03, 2025 3:02 pm I would add this, but due to how blueprints are built I can't send Lua events in the middle of placing the blueprint because mods may invalidate the blueprint which will crash the game.

If anyone ever figures out a way to make it work without mods being able to crash the game then we'll add it.
(feeling weird trying to explain to a developer about their own code... ;-) )

The way I see it, a Blueprint is an ItemStack which in turn can provide a list of BlueprintEntity objects (get_blueprint_entities()). When it is "stamped down", each of those entities (or their equivalent from the ItemStack) is scheduled for construction by placing a ghost (which in turn receives the tags from the blueprint entity).

If in a place where a ghost is 'stamped down', a matching entity exists, then the current code already applies pieces from the Blueprint entity to the entity that exists (most likely the control behavior because e.g. an arithmetic combinator gets reconfigured when a blueprint is placed on top of it.

At this moment, fire an event (most likely a new event) that contains
- the BlueprintEntity that is about to be placed down
- the LuaEntity that exists in that spot

That way, a mod can receive that event. For my mods, I could now inspect the BlueprintEntity, pull out the tags (they represent the configuration of my custom entities) and apply that to the existing LuaEntity. So the "reconfigure when a blueprint is stamped down" would work for me.

I very much appreciate the concern that you voice about "blueprint modes may invalidate the blueprint". I am not sure how that would apply here, given that the process of "stamping down" currently fires no events. And even if firing during the "stamping down" is not possible, it should be doable to "collect" the information about BlueprintEntity / LuaEntity pairs and then fire those after the blueprint placement is complete.

(note that all of this comes from a very naive understanding on how blueprinting works as seen from the outside).
protocol_1903
Fast Inserter
Fast Inserter
Posts: 182
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Re: copy-paste by blueprint should trigger an event

Post by protocol_1903 »

Seems weird that the only stopping point for implementation is mods being able to crash the game. If i understand correctly, then the only way a blueprint could be invalidated is if it's changed, right? So why not make the blueprint read-only, call it a LuaTemporaryBlueprint, so that players can get info from it but not override it.

Also, would it be crashing the game to desktop? Or just a crash to menu? If it's the latter, it should be up to the developer to not crash the game, but I presume that's not the case.
If you need to reach me, message me on discord.

I make qol mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903
If you have a mod idea, I can look into it.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14942
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: copy-paste by blueprint should trigger an event

Post by Rseding91 »

Mods can access anything during a given event - regardless of what is passed in the event parameters.

During the event, a mod could go to the character and call player.character.cursor_stack.clear() which would instantly delete and invalidate the entire blueprint and all entities within it.
If you want to get ahold of me I'm almost always on Discord.
protocol_1903
Fast Inserter
Fast Inserter
Posts: 182
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Re: copy-paste by blueprint should trigger an event

Post by protocol_1903 »

Then save the blueprint item to memory before calling pre_placed_blueprint and use that item regardless when actually placing the blueprint, which would keep it in line with most other events pre_ and on_ running regardless of script actions. Or add a nil check when placing the blueprint and crash to menu.
If you need to reach me, message me on discord.

I make qol mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903
If you have a mod idea, I can look into it.
curiosity
Filter Inserter
Filter Inserter
Posts: 579
Joined: Wed Sep 11, 2019 4:13 pm
Contact:

Re: copy-paste by blueprint should trigger an event

Post by curiosity »

protocol_1903 wrote: Tue Feb 04, 2025 4:27 pm Also, would it be crashing the game to desktop? Or just a crash to menu? If it's the latter, it should be up to the developer to not crash the game, but I presume that's not the case.
The latter is not a crash, just a script error. That would not have been a problem.
protocol_1903 wrote: Tue Feb 04, 2025 4:40 pm Then save the blueprint item to memory before calling pre_placed_blueprint and use that item regardless when actually placing the blueprint, which would keep it in line with most other events pre_ and on_ running regardless of script actions. Or add a nil check when placing the blueprint and crash to menu.
Would it? Entity events stop if you delete the entity, for example. And a script could change blueprint contents too, it doesn't have to delete the blueprint outright.
Post Reply

Return to “Modding interface requests”