Iterating through all entities connected to a circuit network is fairly slow (~2.5ms on my midrange box for a 100 entity network, not something you want to do unnecessarily), but is currently the only reasonable way to detect wiring changes..
If each circuit network (by network_id) had a connection_change_count incremented each time a connection to it was placed or cut, mods could just check that count and almost always avoid re-iterating the entire network. Mods that wanted to manage circuit-attached e.g. inventories or lamps or inserters or train stops could just check the network id and change count their list was built for against the network id and change count in any attached entity's LuaCircuitNetwork, if they don't match it's time to rebuild the cache, otherwise the cache is good.
Add connection_change_count or similar to LuaCircuitNetwork
Re: Add connection_change_count or similar to LuaCircuitNetwork
Why not just an on_wire_connected/disconnected or similar event?
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Add connection_change_count or similar to LuaCircuitNetwork
on_wire_placed has been requested several times from memory. Rsed already said he's not against it. The problem seems to be that nobody has an example good enough to motivate him to actually implement it ;).
Re: Add connection_change_count or similar to LuaCircuitNetwork
Pretty much.eradicator wrote:on_wire_placed has been requested several times from memory. Rsed already said he's not against it. The problem seems to be that nobody has an example good enough to motivate him to actually implement it.
Additionally: a "connection changed" event for the circuit network probably isn't going to happen. When you connect 2 circuit networks or disconnect 2 circuit networks it destroys one of the two networks which triggers the connection-changed logic for every entity in one of the networks twice.
If you want to get ahold of me I'm almost always on Discord.
Re: Add connection_change_count or similar to LuaCircuitNetwork
Yeah, that multiplying overhead was the reason I didn't like the event, there's no reason to even look at most wiring events, only the ones that touch a network some mod is interested in -- and then only to drive rebuilding its cache, so what's the point of firing the event at every wire connection? If the mod's not already running anyway there's no reason to drive it, if it is running just a single check of the network's id/change count against the cached values is enough and I expect far cheaper than driving an event even just once, let alone the flood that could fire when you drop a blueprint.
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: Add connection_change_count or similar to LuaCircuitNetwork
Btw, mini-loader actually implemented on_wire_changed on the lua side (github). I think he uses it because mini loaders are internally two hidden inserters and he needs to make every wire connect to both of them with just one click from the player. Not sure if that is example enough.Rseding91 wrote:Pretty much.eradicator wrote:on_wire_placed has been requested several times from memory. Rsed already said he's not against it. The problem seems to be that nobody has an example good enough to motivate him to actually implement it ;).
Re: Add connection_change_count or similar to LuaCircuitNetwork
From a quick glance at the code it looks like it only works if the wire is disconnected for a full tick or connected for a full tick. If the wire is changed as in removed and added again in the same tick it will miss it (not surprising).eradicator wrote:Btw, mini-loader actually implemented on_wire_changed on the lua side (github). I think he uses it because mini loaders are internally two hidden inserters and he needs to make every wire connect to both of them with just one click from the player. Not sure if that is example enough.Rseding91 wrote:Pretty much.eradicator wrote:on_wire_placed has been requested several times from memory. Rsed already said he's not against it. The problem seems to be that nobody has an example good enough to motivate him to actually implement it.
If you want to get ahold of me I'm almost always on Discord.