Storing Custom Metadata on Entities

Place to get help with not working mods / modding interface.
y_e_a_r
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed Jun 07, 2023 10:38 pm
Contact:

Storing Custom Metadata on Entities

Post 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!
clbhn
Manual Inserter
Manual Inserter
Posts: 4
Joined: Sat Jul 27, 2024 3:29 am
Contact:

Re: Storing Custom Metadata on Entities

Post 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?
y_e_a_r
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed Jun 07, 2023 10:38 pm
Contact:

Re: Storing Custom Metadata on Entities

Post 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.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3723
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Storing Custom Metadata on Entities

Post 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.
y_e_a_r
Burner Inserter
Burner Inserter
Posts: 10
Joined: Wed Jun 07, 2023 10:38 pm
Contact:

Re: Storing Custom Metadata on Entities

Post 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?
User avatar
BrainGamer_
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Sun Nov 14, 2021 9:52 pm
Contact:

Re: Storing Custom Metadata on Entities

Post by BrainGamer_ »

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

Re: Storing Custom Metadata on Entities

Post by y_e_a_r »

Thanks! I created a new post here: viewtopic.php?f=28&t=114640.
Post Reply

Return to “Modding help”