post-dependencies-loaded event

Things that we aren't going to implement
Post Reply
PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

post-dependencies-loaded event

Post by PyroFire »

Hi, is it possible to have a post-dependencies-loaded event?

The idea is that dependencies are often there to expand on a particular mod and configure it in some way.
Often this involves remotes, so the child mods will call remotes on the parent mod.

But there doesn't appear to be a way for the parent to know when all the children are finished loading and have called all their remotes that they need to.
Could we have this as an event of some kind please?

My specific use-case is building a table of surface templates from child mods, and then at the end raise an event when the table has finished being built so those dependent mods can update according to the sum/total collection of data immediately after initialization.

Specifically for control stage.
parent: on_init() global.table={} --LoadChildData() end
Children: on_init() remote.call(parent,register,data) end
(after all children loaded) Parent: on_post_dependencies_loaded() raise_event(my_event,global.table) end

The opposite is fine too, an event that is raised after all mods in the dependency tree have been loaded e.g. on_post_parent_dependencies_loaded()

Bilka
Factorio Staff
Factorio Staff
Posts: 3123
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: post-dependencies-loaded event

Post by Bilka »

If a mod is a dependency of your mod, your on_init will be called after the dependencies on_init. This means this event already exists in the form of on_init. Unless I misunderstood you, of course.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

Re: post-dependencies-loaded event

Post by PyroFire »

Bilka wrote:
Sat Nov 16, 2019 4:52 pm
If a mod is a dependency of your mod, your on_init will be called after the dependencies on_init. This means this event already exists in the form of on_init. Unless I misunderstood you, of course.
Sounds right so far.

In order;

1. Parent: on_init() i_have_remotes() data_bank={} end
2. Child_A: on_init() call_remotes_with_nothing(data_a) end
3. Child_B: on_init() call_remotes_after_child_a(data_b) end
4. Child_C: on_init() call_remotes_after_child_b(data_c) end
5. Missing: Parent: on_post_dependencies_loaded() i_have_much_stuff_from_all_children_now() end
???. Missing: Child_A: on_parents_all_dependencies_loaded() parent_has_much_stuff_from_me_and_child_b_and_child_c_now() end
???. Missing: Child_B: on_parents_all_dependencies_loaded() parent_has_much_stuff_from_me_and_child_a_and_child_c_now() end
???. Missing: Child_C: on_parents_all_dependencies_loaded() parent_has_much_stuff_from_me_and_child_a_and_child_b_now() end

Bilka
Factorio Staff
Factorio Staff
Posts: 3123
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: post-dependencies-loaded event

Post by Bilka »

PyroFire wrote:
Sat Nov 16, 2019 6:10 pm
Sounds right so far.

5. Missing: Parent: on_post_dependencies_loaded() i_have_much_stuff_from_all_children_now() end
How is on_post_dependencies_loaded() missing? Its dependencies will load before its own on_init, so just use on_init.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

Re: post-dependencies-loaded event

Post by PyroFire »

Bilka wrote:
Sat Nov 16, 2019 6:35 pm
PyroFire wrote:
Sat Nov 16, 2019 6:10 pm
Sounds right so far.

5. Missing: Parent: on_post_dependencies_loaded() i_have_much_stuff_from_all_children_now() end
How is on_post_dependencies_loaded() missing? Its dependencies will load before its own on_init, so just use on_init.
Sounds like i may have that backwards, but putting a game.print in migrates says parent loads first then children.

Issue is, there is no way to get the sum of information from the side that sent it.
So parent gets {data_a,data_b,data_c} with current initialization
But there is child_d that wants remote.call("parent","give_me_the_total_data"), and there is no way to do that during initialization because child_e might be sending more data.

Neither child_d can raise itself, nor the parent explicitly call to its children, to get the total_data_bank.

Another name for this could be on_post_init, on_post_load and on_post_configuration_changed, which is simply a repeat of the existing functions but raised after the first round is completed.

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: post-dependencies-loaded event

Post by Boodals »

Bilka wrote:
Sat Nov 16, 2019 4:52 pm
If a mod is a dependency of your mod, your on_init will be called after the dependencies on_init. This means this event already exists in the form of on_init. Unless I misunderstood you, of course.
You have it backwards, Bilka. His mod is the one being depended on, so his on_init fires, then the other mod's, then any other mods that lists his as a dependency. He wants a way to then do something after all of the dependencies have loaded.

It's not completely unreasonable, but it has very few uses.

PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

Re: post-dependencies-loaded event

Post by PyroFire »

Here's my current workaround.

If i leave the events.un_tick line in (removes the hook), it causes a desync.

Code: Select all

function planets.on_init()
	planets.init_globals()
end
function planets.on_load()
end
function planets.on_migrate(ev)
	planets.init_globals()
	global.migrated=true
end
function planets.post_dependencies_loaded()
	events.vraise("PostLoaded",{templates=global.templates})
end

function planets.load_tick(tick) -- I wish this didn't need to be on_tick
	if(tick==1)then planets.post_dependencies_loaded() elseif(global.migrated)then planets.post_dependencies_loaded() end
	--events.un_tick(1,0,"load_tick")
end
events.on_tick(1,0,"load_tick",planets.load_tick)


Attempt #2:

Code: Select all

function planets.load_tick(tick) -- I wish this didn't need to be on_tick
	if(tick>0 and global.migrated)then planets.post_dependencies_loaded() global.migrated=false end
	events.un_tick(1,0,"load_tick")
end
--events.on_tick(1,0,"load_tick",planets.load_tick)


function planets.on_init()
	planets.init_globals()
	global.migrated=true
	events.on_tick(1,0,"load_tick",planets.load_tick)
end
function planets.on_load()
end
function planets.on_migrate(ev)
	planets.init_globals()
	global.migrated=true
	events.on_tick(1,0,"load_tick",planets.load_tick)
end
function planets.post_dependencies_loaded()
	events.vraise("PostLoaded",{templates=global.templates})
end

Attempt #3 (more changes to load_tick - i realized running it on 0 would stop it from running on tick 1)

Code: Select all

function planets.load_tick(tick) -- I wish this didn't need to be on_tick
	if(tick>0)then if(global.migrated)then planets.post_dependencies_loaded() global.migrated=false end events.un_tick(1,0,"load_tick") end
end


Another name for this event could be "on_started", which i've read somewhere on these forums before but can't remember where.

Bilka
Factorio Staff
Factorio Staff
Posts: 3123
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: post-dependencies-loaded event

Post by Bilka »

Boodals wrote:
Sat Nov 16, 2019 11:18 pm
His mod is the one being depended on, .... He wants a way to then do something after all of the dependencies have loaded.
Now I'm even more confused: His mod doesnt have dependencies. He wants to do something after its dependencies. What dependencies? Not to mention that on_init already is called after dependencies on_init.

@PyroFire your code tells me nothing of load order or who has dependencies on what and wants to what to who or when. Which seems to be the problem here, but I don't really know because I still have no idea what your scenario even is.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

PyroFire
Filter Inserter
Filter Inserter
Posts: 356
Joined: Tue Mar 08, 2016 8:18 am
Contact:

Re: post-dependencies-loaded event

Post by PyroFire »

I want the equivalent of data_final_fixes but instead of the data phase, it's the control phase initialization and its called on_init_final_fixes and on_configuration_changed_final_fixes.

The problem boils down to detecting when data is removed, because new data can be managed additively.

Right now my best option is to put the mod folder name in the data chunks (as appears in game.active_mods) so i have a state during on_configuration_changed where the child mod can first read everything the parent currently has so far (and the parent has already checked game.active_mods in its data and removed those entries) and sync with parent, then additively raise child-data-registration events.

Basically, i need to put the name of the mod folder in anything in my mod-shared data tables and distribute a small lib.lua to interface with those events and do the "Read first then listen for new data" thing.

But still no way to detect when that data stream is finished, it will instead act like every data chunk is the last one.

This is somewhat difficult to describe, now that theres some workaround for it, i'll be able to show examples of the issues i'm describing in future.

IronCartographer
Filter Inserter
Filter Inserter
Posts: 454
Joined: Tue Jun 28, 2016 2:07 pm
Contact:

Re: post-dependencies-loaded event

Post by IronCartographer »

Boodals wrote:
Sat Nov 16, 2019 11:18 pm
His mod is the one being depended on, so his on_init fires, then the other mod's, then any other mods that lists his as a dependency. He wants a way to then do something after all of the dependencies have loaded.

It's not completely unreasonable, but it has very few uses.
I've been talking with Pyro and I think this entire thread should have referred to post-dependents, as a way of running a single instance of library code instead of every time something interacts with it.

Essentially, being able to respond to configuration changes with a single aggregate event handler in the dependency (library), rather than each dependent forcing a complete rebuild of the metadata created by callbacks into said library.

He's working around it now (so this thread isn't strictly necessary, though potentially interesting for streamlining advanced mod interactions) by tracking more data in the first place, so that the aggregate function can be purely incremental, rather than needing to do destructive operations atomically with the rebuild. I think. He's writing the code, I'm just taking a very abstract view as ever...

Boodals
Fast Inserter
Fast Inserter
Posts: 129
Joined: Sun Feb 11, 2018 7:10 pm
Contact:

Re: post-dependencies-loaded event

Post by Boodals »

Reading back what I wrote.. I probably made it more confusing.
His mod has no (relevant) dependencies.
Other mods list his mod as a dependency. They get on_init called after his mod.
He wants an init event which will fire after all of the [mods that list his as a dependency] inits fire.

Post Reply

Return to “Won't implement”