Storing Custom Metadata on Entities
Posted: Thu Jul 04, 2024 5:11 pm
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:
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!
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
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!