LuaCircuitNetwork.signals_changed

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3754
Joined: Tue May 13, 2014 11:06 am
Contact:

LuaCircuitNetwork.signals_changed

Post by DaveMcW »

I would like a read-only boolean property on LuaCircuitNetwork to tell when the contents of a circuit network have changed. I believe this already exists internally, it just needs to be exposed to Lua.

The use case is controlling modded buildings through the circuit network. With a fast way to see if a circuit network has changed, a mod can skip the time-consuming comparison of individual signals on ticks where nothing happened.
User avatar
Hares
Filter Inserter
Filter Inserter
Posts: 904
Joined: Sat Oct 22, 2022 8:05 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Hares »

Great suggestion.
+1 on that
Fulgora is the best planet. Vulcanus needs rework. Feel free to prove me wrong.
Rseding91
Factorio Staff
Factorio Staff
Posts: 17169
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Rseding91 »

Changed compared to what?
If you want to get ahold of me I'm almost always on Discord.
Nidan
Filter Inserter
Filter Inserter
Posts: 378
Joined: Sat Nov 21, 2015 1:40 am
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Nidan »

Judging from the final sentence, compared to the previous tick.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 4681
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by boskid »

Such value is indeed available but it is not save-load stable and as such i am not allowed to expose that without risks of desyncs. Due to way circuit network is implemented in 1.1, it is recomputing some values during first update after save was loaded (by client) even when those computations are not required on server. There are also some annoying behaviors related to this value as it may be spuriously set by some entities (i think roboport may be one of them) every tick even if the signals are not changing so it may not be useful in some cases. I may reconsider this once 1.2 would be released since the circuit network in 1.2 is implemented slightly differently causing less spurious notifications about changed signals and the value in 1.2 is save-load stable making it possible to be exposed here.
braxbro
Long Handed Inserter
Long Handed Inserter
Posts: 54
Joined: Sun Jan 01, 2023 2:54 am
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by braxbro »

DaveMcW wrote: Tue May 25, 2021 10:55 am I would like a read-only boolean property on LuaCircuitNetwork to tell when the contents of a circuit network have changed. I believe this already exists internally, it just needs to be exposed to Lua.
Why not make an event out of it?
mrvn
Smart Inserter
Smart Inserter
Posts: 6002
Joined: Mon Sep 05, 2016 9:10 am
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by mrvn »

braxbro wrote: Sun Apr 16, 2023 10:15 pm
DaveMcW wrote: Tue May 25, 2021 10:55 am I would like a read-only boolean property on LuaCircuitNetwork to tell when the contents of a circuit network have changed. I believe this already exists internally, it just needs to be exposed to Lua.
Why not make an event out of it?
That would generate an event for every single circuit network that has a change. You can't just listen to events on a specific circuit network (which would be really great).

Maybe there could be a new CircuitNetworkListener prototype, a new entity, that generates events. That way you would only get events where that entity is placed and connected.
User avatar
Hares
Filter Inserter
Filter Inserter
Posts: 904
Joined: Sat Oct 22, 2022 8:05 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Hares »

mrvn wrote: Sun Apr 16, 2023 11:19 pm That would generate an event for every single circuit network that has a change. You can't just listen to events on a specific circuit network (which would be really great).

Maybe there could be a new CircuitNetworkListener prototype, a new entity, that generates events. That way you would only get events where that entity is placed and connected.
I suppose an per-entity or per-entity-prototype event could also be a soultion, since it is the most-used case.
Plus, unlike per-network event, this will allow not worrying about player rewiring their stuff or scripts performing multiple (un)wire actions on the same tick.
Fulgora is the best planet. Vulcanus needs rework. Feel free to prove me wrong.
scruffyvoltherder
Inserter
Inserter
Posts: 34
Joined: Fri Jun 14, 2024 12:33 am
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by scruffyvoltherder »

+1

This would be very useful
User avatar
Yodo
Long Handed Inserter
Long Handed Inserter
Posts: 94
Joined: Sat Dec 30, 2023 4:27 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Yodo »

Any chance for Factorio 2.1?
Have you considered using flow routers instead of balancers?
Roboport + Land mines > Behemoth Worm > (Legendary) Flamethrower turret
User avatar
protocol_1903
Filter Inserter
Filter Inserter
Posts: 520
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by protocol_1903 »

I'm working on a mod library for this use case since i doubt it will see proper implementation in the engine. Will be coming soon, hopefully.

mod link for those interested
pY and pYblock developer, wielder of fluid networks and subtick events in arbitrary ways. I make mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903

Buy me a coffee
User avatar
Hares
Filter Inserter
Filter Inserter
Posts: 904
Joined: Sat Oct 22, 2022 8:05 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Hares »

protocol_1903 wrote: Thu Jan 08, 2026 6:02 am I'm working on a mod library for this use case since i doubt it will see proper implementation in the engine. Will be coming soon, hopefully.

mod link for those interested
I appreciate your determination.
However, the whole purpose for this request is to not perform on_tick events for each monitored network.
Fulgora is the best planet. Vulcanus needs rework. Feel free to prove me wrong.
User avatar
Yodo
Long Handed Inserter
Long Handed Inserter
Posts: 94
Joined: Sat Dec 30, 2023 4:27 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Yodo »

Hares wrote: Thu Jan 08, 2026 8:38 am However, the whole purpose for this request is to not perform on_tick events for each monitored network.
I'm pretty sure this request won't change that. You'll still have to check the value of is_circuit_network_changed every tick, it's not an event.
Have you considered using flow routers instead of balancers?
Roboport + Land mines > Behemoth Worm > (Legendary) Flamethrower turret
User avatar
Hares
Filter Inserter
Filter Inserter
Posts: 904
Joined: Sat Oct 22, 2022 8:05 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Hares »

Yodo wrote: Thu Jan 08, 2026 2:04 pm
Hares wrote: Thu Jan 08, 2026 8:38 am However, the whole purpose for this request is to not perform on_tick events for each monitored network.
I'm pretty sure this request won't change that. You'll still have to check the value of is_circuit_network_changed every tick, it's not an event.
Yeah, that's my point. The idea behind this feature request is to subscribe to a specific circuit network to reduce on_tick operations.
Fulgora is the best planet. Vulcanus needs rework. Feel free to prove me wrong.
User avatar
Yodo
Long Handed Inserter
Long Handed Inserter
Posts: 94
Joined: Sat Dec 30, 2023 4:27 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Yodo »

An event on_signals_changed is not the same as a readable value /attribute is_signals_changed, I thought this thread was about the latter.
Have you considered using flow routers instead of balancers?
Roboport + Land mines > Behemoth Worm > (Legendary) Flamethrower turret
User avatar
Hares
Filter Inserter
Filter Inserter
Posts: 904
Joined: Sat Oct 22, 2022 8:05 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by Hares »

Yodo wrote: Thu Jan 08, 2026 2:54 pm An event on_signals_changed is not the same as a readable value /attribute is_signals_changed, I thought this thread was about the latter.
:thinking:
I don't see if it is remotely usable.
Fulgora is the best planet. Vulcanus needs rework. Feel free to prove me wrong.
User avatar
protocol_1903
Filter Inserter
Filter Inserter
Posts: 520
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Re: LuaCircuitNetwork.signals_changed

Post by protocol_1903 »

Hares wrote: Thu Jan 08, 2026 8:38 am
protocol_1903 wrote: Thu Jan 08, 2026 6:02 am I'm working on a mod library for this use case since i doubt it will see proper implementation in the engine. Will be coming soon, hopefully.

mod link for those interested
I appreciate your determination.
However, the whole purpose for this request is to not perform on_tick events for each monitored network.
This is not going to be an on_tick per network. It will be a circuit network monitor that fires events when triggered.
pY and pYblock developer, wielder of fluid networks and subtick events in arbitrary ways. I make mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903

Buy me a coffee
User avatar
Gergely
Filter Inserter
Filter Inserter
Posts: 659
Joined: Sun Apr 10, 2016 8:31 pm
Contact:

Re: Circuit network signal monitoring

Post by Gergely »

Some way to monitor changes would really be appreciated. I am working on adding custom control behaviour to an entity and I found no way for my code to sleep until the circuit inputs to the entity change.

My suggestion is to add a function to LuaCircuitNetwork that enables a monitor on it, causing a new event type to be fired every time the signals on the network change. And possibly with support for signal filters because many control behaviours only use one signal or one signal kind. (Set value controls use specific signals, while something like setting an item filter only cares about item signals.)

Because this Lua object is associated with a connection on an entity, if the connection is removed it could be deregistered automatically. If we combine this with the suggestion to add wire connected/disconnected events (698828) it would allow the scripted control behaviour to be completely passive.
User avatar
protocol_1903
Filter Inserter
Filter Inserter
Posts: 520
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Re: Circuit network signal monitoring

Post by protocol_1903 »

Gergely wrote: Sat Jul 11, 2026 8:23 pm My suggestion is to add a function to LuaCircuitNetwork that enables a monitor on it, causing a new event type to be fired every time the signals on the network change. And possibly with support for signal filters because many control behaviours only use one signal or one signal kind. (Set value controls use specific signals, while something like setting an item filter only cares about item signals.)
Simply, no. Events will not be fired 'because an object exists' because the object doesnt exist, it just is a reference to a luaobject that may or may not exist. Additionally, having conditional event firings will break determinism with the mod which is a no-go. Also also, mods cannot have events fired just for that mod. Events are always fired, for every mod.

You could have some method of registering a listener to a specific circuit network which would fire said events (similar to script.register_on_object_destroyed), but there wouldn't be an object associated with it and it would fire events for every mod.
pY and pYblock developer, wielder of fluid networks and subtick events in arbitrary ways. I make mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903

Buy me a coffee
User avatar
Gergely
Filter Inserter
Filter Inserter
Posts: 659
Joined: Sun Apr 10, 2016 8:31 pm
Contact:

Re: Circuit network signal monitoring

Post by Gergely »

First I would like to note that 698828 precedes this. There is no point to monitoring signals if we cannot even tell when there is something to monitor in the first place.

With that out of the way, let me get down to proposing an actual API:

New function: LuaCircuitNetwork::register_on_change
New event: on_circuit_network_signals_changed.

Calling register_on_change sets a flag on the LuaCircuitNetwork object: The next time the signals change, (the values returned by LuaCircuitNetwork::signals change) the flag is cleared and the on_circuit_network_signals_changed event is fired for all mods that listen to it. The event data will contain a reference to the LuaCircuitNetwork object, enabling retrieval of the owning entity.

The flag persists through a save and load and it is not possible to clear it from the script side. It is cleared before the event is fired however, allowing mods to independently reassess whether they still need to listen to updates and set it again if they do.

Why fire the event for every mod? Well every event in the game is fired for every mod that listens and this should be no different. Mods have to filter events depending on what information they actually care about. Also, like all events I am not proposing to make this event registration somehow persist between save and load. Mods have to reregister it like all events. However the flag on the LuaCircuitNetwork object should persist so mods do not have to track every object they care about in storage and set the flags again in on_load. (Where such code would most likely go.)

Why no flag-per-mod? Because checking one flag per mod is slow and could complicate memory management. I cannot make any claims on what kind of optimizations are possible. One flag seems simple. However it has to behave the way it does to avoid interference between mods. Multiple mods can set it, which is still equivalent to setting it once. Clearing it before the event gives an opportunity for all mods to individually decide whether they still want to listen to the event on the particular object.

One flag per SignalIDType could be considered. The idea is simple: register_on_change accepts an optional SignalIDType and only sets the corresponding flag. All flags by default. Signal changes are considered per SignalIDType: A flag is only cleared when a signal with the corresponding type changes and the event is only fired when any flag is cleared. An EventFilter could be added to filter on the type of signals affected or even on the name/type of the entity the LuaCircuitNetwork belongs to. This is useful in case a script-defined behaviour only cares about one type of signal. For example a script-defined item filter will only care about items, a fluid filter only cares about fluids and so on.
Post Reply

Return to “Modding interface requests”