LuaEntity::register_on_signal_changed

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 474
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

LuaEntity::register_on_signal_changed

Post by Stringweasel »

TL;DR
Add a way to register an a specific entity (or it's control behaviour) to raise an event when the signal input changes (and optionally passes some conditions ). Similar to how register_on_object_destroyed works. Something like:

Code: Select all

---Lua psuodo declaration of function to register a specific entity for signal changed events.
---@param conditions DeciderCombinatorCondition[]? 
---@return uint64 registeration number
function LuaEntity.register_on_signal_changed(filters)
    -- ...
end
And then when the signal going to this entity changes (and optionally passes the conditions) it raises an event like:

Code: Select all

---@class EventData.OnSignalChanged
---@uint64 registration_number
Example Usage

Code: Select all

script.on_event(defines.events.on_built_entity, function (event)
    local registration_number = event.entity.register_on_signal_changed()
    storage.entity_signal_listeners[registration_number] = event.entity
end)

script.on_event(defines.events.on_signal_changed, function (event)
    local entity = storage.entity_signal_listeners[event.registration_number]
    if not entity then return end -- We don't care about this event
    game.print("Signal update with: "..entity)
end)

Why
Maybe this approach will not run into the same concerns other similar requests had about performance impact or too much events firing (for example 75678, 104311, 99990). The events will only ever fire for that small handful of entities that are registered. This could probably also be implemented in a way that doesn't increase the memory footprint of an entity.
Other requests
Here is an alterative request by DaveMcW that could also be nice: 98542

There also is another request that's very similar to mine if I read it correctly: 123158
Alt-F4 Author | Factorio Modder
Probably known for: (Configurable) Valves | Better Victory Screen | Space Spidertron | Fluidic Power
Official Contributor to Space Exploration
User avatar
protocol_1903
Filter Inserter
Filter Inserter
Posts: 356
Joined: Fri Sep 09, 2022 4:33 pm
Contact:

Re: LuaEntity::register_on_signal_changed

Post by protocol_1903 »

+1. This is doable via some truly cursed scripting but a much more performant option would be welcome.
Py and PyBlock developer, wielder of LUA in arbitrary ways. I make mods. Check them out, maybe.
https://mods.factorio.com/user/protocol_1903
Post Reply

Return to “Modding interface requests”