I got it working
-- Changes made:
-- 1. on_tick is permanently registered.
-- 2. Removed dynamic script.on_event(on_tick, ...) registration/unregistration.
-- 3. Removed script.on_load() registration.
-- 4. on_tick immediately returns when no dirty surfaces exist.
-- Apply these edits to your original file:
-- In mark_surface_dirty(), REMOVE:
-- script.on_event(defines.events.on_tick, on_tick)
-- Replace your on_tick function with:
on_tick = function()
ensure_storage()
if next(storage.dirty_surfaces) == nil then
return
end
rebuild_if_startup_settings_changed()
for surface_index in pairs(storage.dirty_surfaces) do
local surface = game.get_surface(surface_index)
if surface then
update_surface(surface)
end
end
storage.dirty_surfaces = {}
end
-- DELETE this entire block:
-- script.on_load(function()
-- script.on_event(defines.events.on_tick, on_tick)
-- end)
-- Finally, add this ONCE near the bottom of the file
-- (after all function definitions is fine):
script.on_event(defines.events.on_tick, on_tick)