Why is on_configuration_changed not running?

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

Why is on_configuration_changed not running?

Post by quyxkh »

I'm getting a very weird symptom: a scenario map's on_configuration_changed does not run if loaded when its mod is missing, and it's got something to do with remote interfaces. I've included a fairly minimal reproducer, I'm posting here on the theory that I must be doing something wrong. The o-c-c function is called properly for istm any change that doesn't involve removing that interface.

Here's the entirety of the mod's control.lua:

Code: Select all

function testocc_interface()
	print('testocc-interface')
end
remote.add_interface('testocc',{t_i=testocc_interface})
and the non-scaffolding parts of the attached scenario's control.lua:

Code: Select all

function configuration_changed(ccdata)
    print(serpent.block(ccdata or 'o-c-c from on-init'))
    do local _=remote.interfaces.testocc or {}
        global.use_testocc_ti = _.t_i or false
        print(_.t_i or false)
        end
    end
function conditionalhooks()
    if global.use_testocc_ti then
        for x=1,#player do
            remote.call('testocc','t_i'
                    ,{player_index=x})
            end
    else
        on('player_left_game')
        end
    end
for k,v in next,_G do npg[k]=true end

use_testocc_ti = {} -- placeholder
player={}
on('configuration_changed',configuration_changed)
on('load',function()
    for k,v in next,global do 
        print('Global "'..k..'" restored')
        _G[k]=v end
    conditionalhooks()
    end)

on('init',function(...)
    for k,v in next,_G do if not npg[k] then
        print('Global "'..k..'" persisted')
        global[k]=v end end
    configuration_changed()
    -- don't know c-c changing the lua globals is ok so just reload from map globals 
    for k,v in next,global do _G[k]=v end
    conditionalhooks()
    end)
Attachments
test-o-c-c.zip
The map
(1.57 MiB) Downloaded 46 times
test-o-c-c_0.0.1.zip
The mod
(2.1 KiB) Downloaded 34 times
Rseding91
Factorio Staff
Factorio Staff
Posts: 14591
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Why is on_configuration_changed not running?

Post by Rseding91 »

Code: Select all

on('init',function(...) ...

... why? Why obscure what's perfectly clear logic that 95%+ of the mods do and any beginner can find documentation on behind functions like this?

Code: Select all

script.on_init(function(...) ... end)
is perfectly clear what it's doing and anyone new to Factorio modding will immediately know what it does because that's how the official documentation says to do it.
If you want to get ahold of me I'm almost always on Discord.
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Why is on_configuration_changed not running?

Post by quyxkh »

... yah, I thought that might be a distraction, so I made and have attached a really minimalized test-case.
digression
I've attached a scenario-only mod, and a service mod that provides a remote-call interface. To reproduce what I'm looking at,

1. Run with only the test-o-c-c-scenario mod (and any others except the other attached one), and play and save the map with the test-o-c-c-scenario/test-o-c-c scenario. It prints messages to the console, those might be helpful.

2. Bang on mod presence/version changes (*including* the attached test-o-c-c-service) you want, you can see the scenario's o-c-c- runs fine.

3. Save the map after loading it with the test-o-c-c-service mod present. Make any changes you want except removing the test-o-c-c-service mod, this save's o-c-c still runs fine, you can see the serpent.block printout of the args on the console.

4. Reload the save from step 3 without the test-o-c-c-service mod.

There's the part I don't understand: loading fails with an error in on_load, but it appears on_configuration_changed never ran.
service control.lua
scenario control.lua
Attachments
test-o-c-c-scenario_0.0.2.zip
The scenario that uses the remote interface in test-o-c-c-service if it's present.
(3.37 KiB) Downloaded 40 times
test-o-c-c-service_0.0.2.zip
The mod whose presence the scenario's saves develop an odd dependence on
(1.07 KiB) Downloaded 40 times
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Why is on_configuration_changed not running?

Post by darkfrei »

Rseding91 wrote:

Code: Select all

on('init',function(...) ...
... why? Why obscure what's perfectly clear logic that 95%+ of the mods do and any beginner can find documentation on behind functions like this?

Code: Select all

script.on_init(function(...) ... end)
is perfectly clear what it's doing and anyone new to Factorio modding will immediately know what it does because that's how the official documentation says to do it.
What if I want to call "on_init" twice? Is it possible to register every "on_init" calling?
quyxkh
Smart Inserter
Smart Inserter
Posts: 1031
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: Why is on_configuration_changed not running?

Post by quyxkh »

Well, the problem was that I somehow got it in my head that on_configuration_changed runs before on_load, which doesn't match any version of docs or reality that I can find now. on_configuration_changed runs after on_load.
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Why is on_configuration_changed not running?

Post by bobingabout »

darkfrei wrote:
Rseding91 wrote:

Code: Select all

on('init',function(...) ...
... why? Why obscure what's perfectly clear logic that 95%+ of the mods do and any beginner can find documentation on behind functions like this?

Code: Select all

script.on_init(function(...) ... end)
is perfectly clear what it's doing and anyone new to Factorio modding will immediately know what it does because that's how the official documentation says to do it.
What if I want to call "on_init" twice? Is it possible to register every "on_init" calling?
I've tried that before, the second call over-writes the first. There can only be one on_init.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
chrisgbk
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Mon Jan 02, 2017 4:31 am
Contact:

Re: Why is on_configuration_changed not running?

Post by chrisgbk »

darkfrei wrote:
Rseding91 wrote:

Code: Select all

on('init',function(...) ...
... why? Why obscure what's perfectly clear logic that 95%+ of the mods do and any beginner can find documentation on behind functions like this?

Code: Select all

script.on_init(function(...) ... end)
is perfectly clear what it's doing and anyone new to Factorio modding will immediately know what it does because that's how the official documentation says to do it.
What if I want to call "on_init" twice? Is it possible to register every "on_init" calling?
The accepted method is to register a single on_init stub that calls every on_init function you want called.

Code: Select all

script.on_init(function()
  my_init()
  some_other_init()
end)
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Why is on_configuration_changed not running?

Post by darkfrei »

chrisgbk wrote:The accepted method is to register a single on_init stub that calls every on_init function you want called.

Code: Select all

script.on_init(function()
  my_init()
  some_other_init()
end)
It would be nice to have every script function in vanilla like this:

Code: Select all

script.on_init(function()
  for i, on_init_handler in pairs (on_init_handlers) do
    on_init_handler() -- all on_init functions in control.lua
  end
end)
So, if you must register your function in the list of functions.
Post Reply

Return to “Modding help”