Lua handlers by load order
When the core Lua API was being designed, care was given to engine and event handlers triggering (mostly) in load order, with the exception of dynamically attached scripts.
As we dehardcode mechanics, we'll have more and more API similar to ItemUsage.addHandlerForObject. There only the sequence in which handlers were registered matters, and thus the script can "override" load order (usually unintentionally) by registering its handler in the body of the script, in onUpdate
, after some delay or event triggers, etc. Changing load order is the main tool for the player to resolve mod conflicts without changing the mods themselves, and it's currently not always useful for Lua mods.
We already have a script context aware Lua entity - async:callback
. We could have a "token" of some kind that represents load order of the .omwscripts/.omwaddon file adding the script (inherited from the global script in case of :addScript
). Then handler registering functions could take it as an argument, and sort the callbacks by it.
It could even be the async:callback
API used for it. Example API:
local usageHandlers = {}
local function registerHandler(callback)
table.insert(usageHandlers, callback)
table.sort(usageHandlers, function(a, b)
return a.loadOrder > b.loadOrder
end)
end