Page 1 of 1

Custom Data

Posted: Sat Jul 23, 2016 12:30 am
by Ranakastrasz
I am trying to add custom data to a particular prototype, so as to carry extra data. It also needs to support multiple mods, for it to work properly.

Approach one is to use a Remote Interface to build a table. However, while it works, I have no idea how to handle mod updates or mod removal. In those cases, the table will no longer be accurate, and unlike data, doesn't get rebuilt. I am unsure how to properly detect and rebuild it correctly, which means it will end up stacking multiple copies of stuff or have entries that should be removed left behind.

Approach two is to use a prototype to carry the information. Since data is global between mods, and easily overwritten, it should work better. However, since you can't just add extra data to prototypes, I would need a dummy prototype which carries this information. It should be possible to, on game load, find all such dummy prototypes and load them into a table to be used. However, I would need a prototype that I can access the proper information for it to work.

Any mod that already handles this kind of problem would be helpful. I looked at treefarm, but I can't figure out how it de-registers or handles updates.

Re: Custom Data

Posted: Sat Jul 23, 2016 1:00 am
by DaveMcW
The best prototype for custom data is the constant combinator. You get 15 integers and 15 item signals.

Re: Custom Data

Posted: Sat Jul 23, 2016 1:08 am
by Ranakastrasz
Not what I meant, I don't think. I don't need to store data in an entity ingame, I need to attach extra data to prototypes.

Re: Custom Data

Posted: Mon Jul 25, 2016 4:44 pm
by DedlySpyder
So, the first idea is to have a base mod that handles the table storage with remote calls that your others ones access to add/remove/etc?

You could handle partial mod removal by having that base mod check active_mods when on_configuration_changed is fired, to remove anything relevant (one of the elements in the table being the mod that added it).

I guess you would also need a meta table to store which mods are actually in use so it would know what it has to check for

Re: Custom Data

Posted: Mon Jul 25, 2016 6:57 pm
by aubergine18
Lua supports weak tables, that will automatically drop entries that are no longer referenced from elsewhere. More info here: http://lua-users.org/wiki/WeakTablesTutorial

Re: Custom Data

Posted: Tue Jul 26, 2016 12:06 pm
by Adil
There's the mod on portal, which allows accessing data.raw from ingame. (Named accordingly. Something like: data.raw...)
I believe any custom field should be carried over by this mod as well.

Re: Custom Data

Posted: Tue Jul 26, 2016 5:04 pm
by mojo2012
You can use the factorio-stdlib API, it does the actual storage in the global context though.