• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Lua] Control the order of the scripts

Status
Not open for further replies.
Level 23
Joined
Jun 26, 2020
Messages
1,838
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:
Lua:
if B then
    function hola()
        print("hola")
    end
end

function B()

end

hola() -- This give an error (nil function)
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.):
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
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:

Lua:
function hola()
    print("hola")
end

hola() -- prints hola

function hola()
    print("adios")
end

hola() -- prints adios
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)?
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,456
We had a big huddle discussion on this here if you are interested:

 
Level 23
Joined
Jun 26, 2020
Messages
1,838
I'm having a problem with the Damage Engine and the Debug Utils, for some reason the triggers with the event "xxxDamageEvent Becomes Equal to 1.00" are not affected by this 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
So they never display errors automatically, the unique reason I can though this happens is because the Debug Utils is positioned right after the Damage Engine in the war3map.lua, or not?
 
Level 19
Joined
Jan 3, 2022
Messages
320
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
You start with a wrong premise. Take a look inside war3map.lua again, triggers and scripts are placed in the order they are defined.
First trigger at the top: placed first.
Last trigger at the bottom: placed last.

WorldEdit does have a few structures that are placed before/after GUI+Trigger code, but as far as your edits go, you have full control over them.

PS: I should publicly release my safeCall wrapper (aka try() aka pcall()). It has the ability to hook every existing function.
 
Level 23
Joined
Jun 26, 2020
Messages
1,838
You start with a wrong premise. Take a look inside war3map.lua again, triggers and scripts are placed in the order they are defined.
First trigger at the top: placed first.
Last trigger at the bottom: placed last.

WorldEdit does have a few structures that are placed before/after GUI+Trigger code, but as far as your edits go, you have full control over them.

PS: I should publicly release my safeCall wrapper (aka try() aka pcall()). It has the ability to hook every existing function.
Lol, I didn't know that, I wasn't aware of how it worked when I was using JASS, because it seemed to order them in "the order you created the triggers", thank you.
 
Level 19
Joined
Jan 3, 2022
Messages
320
Sure it's possible they've changed it with recent WorldEditor changes. And you mentioned Jass. JassHelper's output for vJass is different, it does put some things at the top and few at the bottom (above/below all else) iirc. Lua has no such surprises :peasant-thumbs-up:
 
Level 23
Joined
Jun 26, 2020
Messages
1,838
Sure it's possible they've changed it with recent WorldEditor changes. And you mentioned Jass. JassHelper's output for vJass is different, it does put some things at the top and few at the bottom (above/below all else) iirc. Lua has no such surprises :peasant-thumbs-up:
I was refering to the normal Jass, or simply the triggers, but it seems that with them happens the same.
 
Level 9
Joined
Jul 20, 2018
Messages
176
First trigger at the top: placed first.
Last trigger at the bottom: placed last.
A little correction. New code blocks and new triggers are always placed at the bottom of the script, even if rearranged. But after you reopen map in the editor and save again, all stuff is placed in the order as in the Trigger Editor.
 
Last edited:

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,456
Sure it's possible they've changed it with recent WorldEditor changes. And you mentioned Jass. JassHelper's output for vJass is different, it does put some things at the top and few at the bottom (above/below all else) iirc. Lua has no such surprises :peasant-thumbs-up:
Lua has the surprise that "map header" Lua code is placed below EVERY trigger in the war3map.lua file. It's quite weird. Probably was done that way because JassHelper did it like that, even though it never should have.
 
Level 19
Joined
Jan 3, 2022
Messages
320
"map header" Lua code
What is this?
Lua:
function Trig_IncreaseMonotonicClock_Actions()
    udg_monotonicSeconds = (udg_monotonicSeconds + 1)
end

function InitTrig_IncreaseMonotonicClock()
    gg_trg_IncreaseMonotonicClock = CreateTrigger()
    TriggerRegisterTimerEventPeriodic(gg_trg_IncreaseMonotonicClock, 1.00)
    TriggerAddAction(gg_trg_IncreaseMonotonicClock, Trig_IncreaseMonotonicClock_Actions)
end

function Trig_Melee_Initialization_Actions()
    MeleeStartingVisibility()
    MeleeStartingHeroLimit()
    MeleeGrantHeroItems()
    MeleeStartingResources()
    MeleeClearExcessUnits()
    MeleeStartingUnits()
    MeleeStartingAI()
end

function InitTrig_Melee_Initialization()
    gg_trg_Melee_Initialization = CreateTrigger()
    TriggerAddAction(gg_trg_Melee_Initialization, Trig_Melee_Initialization_Actions)
end

function Trig_MultiboardSetup_Actions()
    CreateMultiboardBJ(1, 5, "TRIGSTR_013")
    udg_multiboard = GetLastCreatedMultiboard()
    MultiboardSetItemColorBJ(GetLastCreatedMultiboard(), 1, 1, 100, 10.00, 0.00, 0)
    MultiboardSetItemValueBJ(GetLastCreatedMultiboard(), 1, 1, "TRIGSTR_014")
    MultiboardSetItemValueBJ(GetLastCreatedMultiboard(), 1, 2, "TRIGSTR_015")
    MultiboardSetItemValueBJ(GetLastCreatedMultiboard(), 1, 3, "TRIGSTR_016")
    MultiboardSetItemValueBJ(GetLastCreatedMultiboard(), 1, 4, "TRIGSTR_017")
    MultiboardSetItemValueBJ(GetLastCreatedMultiboard(), 1, 5, "TRIGSTR_018")
    MultiboardSetItemWidthBJ(udg_multiboard, 1, 1, 10.00)
    MultiboardSetItemWidthBJ(udg_multiboard, 1, 2, 10.00)
    MultiboardSetItemWidthBJ(udg_multiboard, 1, 3, 10.00)
    MultiboardSetItemWidthBJ(udg_multiboard, 1, 4, 10.00)
    MultiboardSetItemWidthBJ(udg_multiboard, 1, 5, 10.00)
    EnableTrigger(gg_trg_MultiboardUpdate)
end

function InitTrig_MultiboardSetup()
    gg_trg_MultiboardSetup = CreateTrigger()
    TriggerRegisterTimerEventSingle(gg_trg_MultiboardSetup, 1.00)
    TriggerAddAction(gg_trg_MultiboardSetup, Trig_MultiboardSetup_Actions)
end

function Trig_MultiboardUpdate_Actions()
    MultiboardSetItemValueBJ(udg_multiboard, 1, 3, I2S(udg_UniqueStringsCreated))
    MultiboardSetItemValueBJ(udg_multiboard, 1, 4, udg_LastStringCreated)
    MultiboardSetItemValueBJ(udg_multiboard, 1, 5, I2S(udg_monotonicSeconds))
end

function InitTrig_MultiboardUpdate()
    gg_trg_MultiboardUpdate = CreateTrigger()
    DisableTrigger(gg_trg_MultiboardUpdate)
    TriggerRegisterTimerEventPeriodic(gg_trg_MultiboardUpdate, 0.50)
    TriggerAddAction(gg_trg_MultiboardUpdate, Trig_MultiboardUpdate_Actions)
end
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,456
For future readers who stumble across this thread via a Google search, the solution to this problem that doesn't require use of a third-party program like TypeScript2Lua:

[Lua] - Global Initialization version 4.0 added the OnLibraryInit mechanism, which allows code to suspend until certain specified variables are declared.
 
Status
Not open for further replies.
Top