Page 1 of 1

Soft-depending mods

PostPosted: Tue Apr 09, 2013 16:54
by Nore
Something that could be useful for lots of mods would be a softdepends.txt file in the mod folder: mods that are listed in it are loaded before the mod if they exist, but else, they are ignored.

PostPosted: Tue Apr 09, 2013 17:29
by webdesigner97
You can use lua to check wheter a certain mod is installed and change your script's behaviour:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
 if minetest.get_modpath("streets") then
  print("Streets is installed")
 else
  print("Streets isn't installed")
 end

I don't think that a softdepend.txt is needed.

PostPosted: Tue Apr 09, 2013 17:32
by Nore
Yes it is, because we sometimes want to execute the code after the other mod in that case (for example in the older versions of mesecons, or if we want to call code in the other mod). Of course, the mod will still call that to know if it should run the code.

PostPosted: Tue Apr 09, 2013 17:41
by stu
webdesigner97 wrote:You can use lua to check wheter a certain mod is installed and change your script's behaviour:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
 if minetest.get_modpath("streets") then
  print("Streets is installed")
 else
  print("Streets isn't installed")
 end

I don't think that a softdepend.txt is needed.


I don't think that is reliable at 'load time' since it appears that mods load in a particular order (probably ascii). Consequently mod 'A' may not be able to detect the presence of mod 'B' until both mods are loaded.

Edit, actually modpath should be still readable, I am thinking of using function calls or static data from other mods.

Please ignore..

PostPosted: Tue Apr 09, 2013 18:21
by prestidigitator
Yes. This would be nice. OR, a way to register a callback that will be made once a named mod is loaded (or called immediately if it is already loaded, or called never if it isn't present). This would eliminate the possibility of introducing circular dependencies where A tries to load after B and B tries to load after A (or A -> B -> C -> A or even more complicated patterns) although it would also require slightly more event-driven thinking. For example, here is how a pair of mods might look with a soft dependency loading order:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
---- Mod A
A = {}
function A.foo(...) ... end

---- Mod B (automatic soft depend on mod B)
A.foo(...)


and here is how it might look with a mod loaded callback:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
---- Mod A
A = {}
function A.foo(...) ... end

---- Mod B
local function aCallback(param)
   A.foo(...)
end
minetest.register_on_mod_loaded("A", aCallback, param)


There are two things worth noting about that example:
  • aCallback may never be called, or may be called right away if mod A is already loaded, so mod B needs to be designed so that either order would work.
  • Since aCallback is never called if mod A isn't loaded, it is free to lookup the global symbol A (which we know is defined by mod A) without testing whether it actually exists.

Mod B in the second example could also look like this, but I wanted to be able to name the function when talking about the behavior:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
---- Mod B
minetest.register_on_mod_loaded("A", function(param) A.foo(...) end, param)

PostPosted: Tue Apr 09, 2013 18:25
by Sokomine
get_modpath seems to be reliable; it just doesn't solve the mentioned problem. Sometimes you have to execute your own code *after* the other mod finished. For example, I want to plug into the tree-growing functions of moretrees. That's difficult to do if those functions are not defined yet because my mod was loaded first. All I could do would be to delay execution of my mod for a few seconds and hope it won't get needed in the meantime.
The callback method prestidigitator suggested would be very helpful.

PostPosted: Wed Apr 10, 2013 12:34
by rubenwardy

PostPosted: Wed Apr 10, 2013 20:05
by Sokomine
When I turned my mod into a modpack, it seems it got executed first :-(. Anyway, doing a callback with 0 seconds as suggested in the thread rubenwardy mentioned may work but isn't exactly a clean soulution.