Upgradeable Quality

by Nor017

building gets quality up through time,only effect when machine is working

Tweaks
8 months ago
2.0
3.48K
Factorio: Space Age Icon Space Age Mod
Cheats

b Found 2 issues with code

29 days ago
(updated 29 days ago)

Hello! I've found a couple of issues with the module upgrade logic and have some suggestions for fixes.

1) Issue: Incompatibility with mod-placed buildings.
Buildings placed or revived by other mods (e.g., via script_raised_revive) are not being registered for upgrades.

Suggested Fix:
In control.lua, at the end of the file, please add the following event handler:

script.on_event(
    defines.events.script_raised_revive,
    On_built_entity
)

This will allow your mod to correctly handle entities created by other mods.

2) Issue: Upgrade logic stops on the first non-upgradable module.

The current can_upgrade_module function checks modules one by one and returns false as soon as it finds a module that cannot be upgraded, even if other modules in the same machine can be upgraded.

Suggested Fix:
In control.lua, please replace the entire can_upgrade_module function with the following corrected version:

function can_upgrade_module(ent)
    if ent.no_module then return end
    local module_inv = ent.entity.get_module_inventory()
    if not module_inv then
        ent.no_module = true
        return false
    end
    local contents = module_inv.get_contents()
    if not contents or next(contents) == nil then return false end

    for _, v in pairs(contents) do
        local next_quality = prototypes.quality[v.quality].next
        if next_quality then.
            if respect_technology then
                if check_quality_unlock(next_quality) then                  
                    return true
                end
            else
                return true
            end
        end
    end

    return false
end

This new logic correctly checks all modules in a machine and will return true if at least one of them can be upgraded.

New response