- Joined
- Jun 26, 2020
- Messages
- 1,921
Hello, I create this thread to discuss a thing that bothers me a lot, you know that all the custom scripts are placed in a unique file war3map.lua, the problem is, we can't control the order in where they will be placed, and this matters since we have libraries that requires other libraries are defined before to work, you know, like doing:
Maybe the solution of that is never define a library like that, but what if a library depends on another library do their work first, like to handling errors for example with [Lua] - Debug Utils (Ingame Console etc.):
But if we define a trigger and then that script is positioned before this, we will never handle a possible error on that trigger, to make it simple:
So, what can we do for that? Something like create a function that calls a function that defines a library (a table with the necessary functions)? And in that case, how can we be sure that function will be defined always before everything we will write (Something like what JassHelper does)?
Lua:
if B then
function hola()
print("hola")
end
end
function B()
end
hola() -- This give an error (nil function)
Lua:
function try(input, ...)
local execFunc = (type(input) == 'function' and input) or load(input)
local results = table.pack(pcall(execFunc, ...)) --second return value is either the error message or the actual return value of execFunc, depending on if it executed properly.
if not results[1] then
print("|cffff5555" .. results[2] .. "|r")
end
return select(2, table.unpack(results, 1, results.n)) --if the function was executed properly, we return its return values
end
--Overwrite TriggerAddAction native to let it automatically apply "try" to any actionFunc.
do
local oldTriggerAddAction = TriggerAddAction
TriggerAddAction = function(whichTrigger, actionFunc)
oldTriggerAddAction(whichTrigger, function() try(actionFunc) end)
end
end
Lua:
function hola()
print("hola")
end
hola() -- prints hola
function hola()
print("adios")
end
hola() -- prints adios