Page 1 of 1

Call function based on installed mods outside of events.

Posted: Wed Sep 11, 2019 6:27 pm
by eradicator
What?
I'd like to call a function f() only if a specific mod M is installed. f() affects the state of my mod (event registry, etc), so it needs to be called each time the mod starts. (Edit: In "control.lua" stage)

What's the problem?
Sounds trivial sure, just:

Code: Select all

if game.active_mods['M'] then script.on_load(f) end
Except "game" is not accessible before or even inside on_load. So i'm at a loss at how to do this. Both mods are my own, so i can do some degree of black magic if someone could tell me the right incantation.

Re: Call function based on installed mods outside of events.

Posted: Wed Sep 11, 2019 6:45 pm
by mrvn
One simple way would be to have public function "enable_M_support" in your first mod. Then in M.on_load() you call that.

Another, ugly way would be to register for on_tick and then in the first tick check game.active_mods['M'] and remove the on_tick.

Re: Call function based on installed mods outside of events.

Posted: Wed Sep 11, 2019 6:52 pm
by eradicator
mrvn wrote: Wed Sep 11, 2019 6:45 pm One simple way would be to have public function "enable_M_support" in your first mod. Then in M.on_load() you call that.
Control stage is not a shared lua state. Remote.call() has the same problem of not being allowed outside events.

mrvn wrote: Wed Sep 11, 2019 6:45 pm Another, ugly way would be to register for on_tick and then in the first tick check game.active_mods['M'] and remove the on_tick.
Not desync safe.

Re: Call function based on installed mods outside of events.

Posted: Wed Sep 11, 2019 6:55 pm
by eradicator
I think i can (ab-)use require, which isn't so bad because i need to require some stuff anyway. But i don't like being forced to use pcall.

Code: Select all

if pcall(require,'__M__/dummy') then f() end

Re: Call function based on installed mods outside of events.

Posted: Wed Sep 11, 2019 7:15 pm
by Choumiko
remote should be available in on_load right? So add a (dummy) remote to mod M, if it's there call f() ?

I remember some quirks with remote and on_init (or on_load) but just checking if it's there should be fine

Re: Call function based on installed mods outside of events.

Posted: Wed Sep 11, 2019 7:27 pm
by mrvn
eradicator wrote: Wed Sep 11, 2019 6:52 pm
mrvn wrote: Wed Sep 11, 2019 6:45 pm One simple way would be to have public function "enable_M_support" in your first mod. Then in M.on_load() you call that.
Control stage is not a shared lua state. Remote.call() has the same problem of not being allowed outside events.

mrvn wrote: Wed Sep 11, 2019 6:45 pm Another, ugly way would be to register for on_tick and then in the first tick check game.active_mods['M'] and remove the on_tick.
Not desync safe.
Damn, this is harder than it sounds.

Re: Call function based on installed mods outside of events.

Posted: Wed Sep 11, 2019 7:35 pm
by Bilka
Use remote interfaces to provide the functions. Then you can check for the existence of the remote interface during on load. You cannot do mod-dependent functions during control lua parsing (without hacks) - aka outside on_load.

Re: Call function based on installed mods outside of events.

Posted: Wed Sep 11, 2019 8:14 pm
by eradicator
Bilka wrote: Wed Sep 11, 2019 7:35 pm Use remote interfaces to provide the functions. Then you can check for the existence of the remote interface during on load. You cannot do mod-dependent functions during control lua parsing (without hacks) - aka outside on_load.
Mmmh. Checking for the *existance* of a remote interface does infact work even during control parsing (before on_load). Though unlike my pcall(require,'') solution it's dependant on load oder. As the main thing i need actually *is* to require a file from M - for which load order doesn't matter - both solutions seem to work for me. Getting rid of the pcall might be nicer though. Any substantial reasons pro/contra each of the solutions?

Code: Select all

if remote.interfaces['M-interface'] then require('__M__/main') end
Btw do you have any clue why active_mods is not available at "script" level but only at "game" level?