on_circuit_network_created

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
aaargha
Filter Inserter
Filter Inserter
Posts: 330
Joined: Wed Dec 07, 2016 8:35 am
Contact:

on_circuit_network_created

Post by aaargha »

TL;DR
Basically on_train_created but for circuit networks.
What ?
I'd like some way for a mod to know whether or not entities have been added/removed from a circuit network. I figured that an event that work like on_train_created and would contain perhaps a LuaCircuitNetwork (set to some entity in the network, I'm not really sure about this one) and one or two network_id for the old networks. It would also be helpful if the event could signal that a circuit network is "destroyed", removing the wire between/removing one of the last two connected entities, this could perhaps be signalled by passing nil as the LuaCircuitNetwork argument and then use one of the old network_id arguments to signal which network was "destroyed".

However, for this to work I think some changes would be needed in how circuit network IDs are allocated: they would have to behave like train IDs, use a new ID every time a change is made to a network.
Why ?
In the general case:
  1. The current way for mods to find out if entities have been added/removed to/from a circuit network the mod wants to interact with is to run a BFS/DFS to list all entities and compare that to an older list to see if changes have been made. This is pretty expensive as it needs to be done in Lua and, depending on the mod idea, you might want to keep track of this for multiple networks in real time.
  2. The current way for a mod to know which entities are connected to any circuit network is to iterate all entities and check if they are connected or not, this needs to be done every time this information is needed. With an event like suggested it could likely be made much more efficient.
Over all I think we might see more mods that utilize circuit connections in interesting ways if it was made easier to interact with using the Lua API.

My use case:
For my mod Automatic train deployment I'm using entities being part of the same circuit network to control which train to copy when spawning new trains. This means that each time a train is to be spawned I currently need to run a BFS to see which entities are connected to the one that should spawn a new train, as this is pretty inefficient, and I'm only really interested in a small subset of the connected entities, I'd like to cache the results to improve performance. The problem I run into is that there is currently, as far as I can see, no way for me to know whether or not an entry in the cache is valid, except doing the BFS which defeats the point of the cache.

For my use case to work I don't really need the event as such, the changed allocation of circuit network IDs would be enough, however, it would make the cache management much simpler though.

Suggestions that are kind of related: viewtopic.php?f=28&t=47979, viewtopic.php?f=28&t=48281

Post Reply

Return to “Modding interface requests”