Original post in "Modding Help": viewtopic.php?f=25&t=114167&p=615620#p615620 that I'm reposting here.
Similar request for Items: viewtopic.php?f=221&t=5851&p=45281&hili ... ata#p45281 that is in "Won't Implement".
Hi,
I'm developing a mod that stores additional metadata on some new entities. For example, I need to store the list of item requests associated with my custom chest entity.
I'm currently doing the "standard" approach used by many other mods like SE where I have a table in global that maps from unit_number to metadata. I also have some helper code that stores the metadata to and from blueprints.
While this approach works for copy/paste, it:
- Requires a lot of boilerplate code
- Doesn't work when pasting blueprints over existing entities
- Doesn't work with undo
I've been blown away by the high quality of modding docs and the lua APIs so this current workaround feels uncharacteristically difficult to implement.
Is there a better way to store custom metadata on entities?
If not, I can think of a few potential approaches that would make it a ton easier to build mods with custom entities.
One approach would be to add an optional "metadata" field to LuaEntity that is persisted across all copy/paste/undo actions. This field ideally would be able to store any custom Lua object that can be serialized in a blueprint.
Another approach I can think of is tags. While tags currently only seem to be supported on ghosts, it would be interesting to see if they could also be supported for normal entities. I'm not sure if there's any additional overhead associated with tags that need to be serialized to blueprints, but it would be amazing if this were possible.
I'm not familiar with the Factorio source code or appetite to make a change like this with all the work on the expansion, but this one change would both remove buggy boilerplate and fix annoying bugs with current implementations for a significant number of mods.
Thanks for your time!
Metadata on Entities
Re: Metadata on Entities
Duplicate of viewtopic.php?f=221&t=104324
Re: Metadata on Entities
Nice find on the duplicate. It's a bummer to see that the previous post was closed as "Won't Implement" but it looks like that thread doesn't address the gaps mentioned here around blueprint pasting and undo.
I totally understand the concerns around memory sizes and separating "core" data from "lua" data but I can't help but think there are ways to address those concerns while implementing this functionality, especially since it's such a common feature of mods.
For example there could be mod-local metadata where the Factorio runtime internally keeps track of the (mod_id, entity_id) -> metadata mapping. This solves the issues:
- Each mod gets its own metadata per entity so different mods don't conflict
- The mapping is stored separately from existing entities so it doesn't affect the memory footprint of entities and is only used when mods opt into the feature.
I totally understand the concerns around memory sizes and separating "core" data from "lua" data but I can't help but think there are ways to address those concerns while implementing this functionality, especially since it's such a common feature of mods.
For example there could be mod-local metadata where the Factorio runtime internally keeps track of the (mod_id, entity_id) -> metadata mapping. This solves the issues:
- Each mod gets its own metadata per entity so different mods don't conflict
- The mapping is stored separately from existing entities so it doesn't affect the memory footprint of entities and is only used when mods opt into the feature.
Re: Metadata on Entities
viewtopic.php?f=28&t=106026 viewtopic.php?f=28&t=103934
You even +1'd to the second one.
None of this solves any issues. Instead it just shoves the work off onto the game. It has all the drawbacks that boskid mentioned.y_e_a_r wrote: ↑Sun Aug 04, 2024 8:46 pmFor example there could be mod-local metadata where the Factorio runtime internally keeps track of the (mod_id, entity_id) -> metadata mapping. This solves the issues:
- Each mod gets its own metadata per entity so different mods don't conflict
- The mapping is stored separately from existing entities so it doesn't affect the memory footprint of entities and is only used when mods opt into the feature.
Also, it sounds suspiciously like a library. So you can write one and release it for other people to use, if one or more don't already exist.
Re: Metadata on Entities
So you're saying that instead of adding support for managing entity metadata, it would be better to focus on adding events like `on_blueprint_paste` and `on_undo` so mods can manage metadata rather than the core game?
Re: Metadata on Entities
I'm saying that's what you are more likely to get, at least with your proposal as it is. Besides, those events would have other uses.
- Stringweasel
- Filter Inserter
- Posts: 404
- Joined: Thu Apr 27, 2017 8:22 pm
- Contact:
Re: Metadata on Entities
This is similar to this post viewtopic.php?p=381580#p381580. It seems like the raising of events during the pasting of blueprints safely is quite complicated on the engine's side.
Alt-F4 Author | Factorio Modder
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |Weasel's Demolition Derby
Official Contributor to Space Exploration
My Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock |
Official Contributor to Space Exploration
Re: Metadata on Entities
No, that's specifically for the recipe change due to blueprint. The blueprint itself would be simpler, if you look at the call stack.Stringweasel wrote: ↑Tue Aug 06, 2024 7:19 amThis is similar to this post viewtopic.php?p=381580#p381580. It seems like the raising of events during the pasting of blueprints safely is quite complicated on the engine's side.