Where to store extra data needed by an entity...

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
User avatar
Omnifarious
Filter Inserter
Filter Inserter
Posts: 267
Joined: Wed Jul 26, 2017 3:24 pm
Contact:

Where to store extra data needed by an entity...

Post by Omnifarious »

I'm thinking of modifying the wireless signals mod to try to remove some bugs and greatly increase it's performance. One aspect of that would be to build a minimum spanning tree for each transmitter describing the path to each receiver it can reach. Directly is easy of course, but I'm more concerned with handling relays.

This graph would be rebuilt whenever a receiver, transmitter or relay was constructed. I would also want to be able to update it when the 'powered' state of a relay changed. When the powered state of a transmitter changed, it can just change to transmit a null signal. And when a receiver loses power, it can just stop forwarding the signal it receives to the connected circuit networks.

I guess the graph data should be stored in some sort of data structure associated with an active transmitter.

The problem is, I've never written Lua. I know Python, C++, Scheme, FORTRAN, Java, and a host of other languages. But not Lua.

So, I'm not sure how to do this. Would it be possible to add a table of some sort to the transmitter entity? How would that look?

If not, would I have to create a global table where you could look up sub-tables by transmitter entity id?

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Where to store extra data needed by an entity...

Post by DaveMcW »

Omnifarious wrote:If not, would I have to create a global table where you could look up sub-tables by transmitter entity id?
You can't add new properties to game entities, so a global table is the best way to go. Factorio provides a magic table named "global" that handles serialization for you. Also, learn Lua in 15 Minutes

Code: Select all

if (not global.transmitter) then global.transmitter = {} end
if (not global.transmitter[entity.unit_number]) then global.transmitter[entity.unit_number] = {} end
local mydata = global.transmitter[entity.unit_number]
mydata.custom_property = "stuff"

Post Reply

Return to “Modding discussion”