Page 1 of 1

Storing Custom Metadata on Entities

Posted: Thu Jul 04, 2024 5:11 pm
by y_e_a_r
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!

Re: Storing Custom Metadata on Entities

Posted: Sat Jul 27, 2024 3:15 pm
by clbhn
https://lua-api.factorio.com/latest/cla ... nit_number is a unique number identifying this entity for the lifetime of the save. Maybe you can store by unit_number in a table?

Re: Storing Custom Metadata on Entities

Posted: Fri Aug 02, 2024 4:16 pm
by y_e_a_r
I'm already using unit number with a table. See the above post for the scenarios in which that approach doesn't work.

Re: Storing Custom Metadata on Entities

Posted: Sat Aug 03, 2024 4:31 pm
by DaveMcW
y_e_a_r wrote: Thu Jul 04, 2024 5:11 pm
  • Doesn't work when pasting blueprints over existing entities
  • Doesn't work with undo
Unfortunately, these are limitations in the modding API.

The workaround is to disable copy/paste for your entity.

Re: Storing Custom Metadata on Entities

Posted: Sun Aug 04, 2024 2:28 pm
by y_e_a_r
I was worried that is the case. I definitely need support for copy/paste so disabling isn't an option. Do the Factorio developers take API feature requests?

Re: Storing Custom Metadata on Entities

Posted: Sun Aug 04, 2024 3:49 pm
by BrainGamer_

Re: Storing Custom Metadata on Entities

Posted: Sun Aug 04, 2024 4:40 pm
by y_e_a_r
Thanks! I created a new post here: viewtopic.php?f=28&t=114640.