on_configuration_changed + attempt to index upvalue '_ENV' ?

Place to get help with not working mods / modding interface.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1032
Joined: Sun May 08, 2016 9:01 am
Contact:

on_configuration_changed + attempt to index upvalue '_ENV' ?

Post by quyxkh »

I'm working on a benchmarking/testing scenario file and mod. When the mod's present, I want to use its event hooks which it publishes with a remote interface. When the mod's absent I want to use the fallback event hooks in the scenario's control.lua.

Running, without the mod present, a scenario saved with the mod present, aborts in the on_configuration_changed hook, with the title error.

Code: Select all

function configuration_changed(...)
    if (remote.interfaces.BenchmarkProduction or empty).insta
    then
        on('marked_for_deconstruction')
        on('built_entity')
        for n = 1,#game.players do
            remote.call('BenchmarkProduction','insta',{player_index=n})
            end
    else 
        on('marked_for_deconstruction',instakill)
        on('built_entity',instabuild)
        end
    end
on('configuration_changed',configuration_changed)
the log message is

Code: Select all

------------- Error -------------
Error while running on_configuration_changed: /home/quyxkh/.factorio/temp/currently-playing/control.lua:26: attempt to index upvalue '_ENV' (a nil value)
---------------------------------
and when I look in the save files's control.lua that's the `local e=defines.events[on_s]` line in

Code: Select all

local bootstraps={init=1,load=1,configuration_changed=1}
function on (s,f)
    local on_s ='on_'..s
    local e = defines.events[on_s]
    if e then
        notrace[e]=true
        return script.on_event(e,f)
    elseif bootstraps[s] then
        return script[on_s](f)
    else -- a custom input
        return script.on_event(s,f)
        end
    end
and I don't understand that at all.

The api docs say
. control.lua script.on_configuration_changed()
When mods are changed (prototypes added or removed), the major game version changes, a mod version changes, a mod is removed, or a mod is added the script.on_configuration_changed() event is fired for each mod subscribed to that event.

This is the main place for handling mod internal data structure changes. Access to the global table, game table and game state are available and can be changed in any way seen fit by the mod.
Will someone please point out what I'm missing here? If I can't check in on_load and can't check in on_configuration_changed, when can I check? Or alternately, if o-c-c is the right place, how should I be doing it?
quyxkh
Smart Inserter
Smart Inserter
Posts: 1032
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: on_configuration_changed + attempt to index upvalue '_ENV' ?

Post by quyxkh »

I don't know enough to more than suspect that deferring the hook setting I had in o-c-c to on_load time, using on_init and on_configuration_changed to set globals and having both load and init call a common doconditionalinit function, is the "right" way to do this, but it worked for me. The scenario does the remote.call into the mod if present and uses its own hooks otherwise, just as desired.
Post Reply

Return to “Modding help”