• 🏆 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] [Deprecated] Variable Event Replacement

Damage Engine, Unit Indexer, Unit Event, Is Unit Moving all rely on variable events. The most obvious solution is for me to externalize this hook to avoid running into conflicts.

The below code has been updated to reflect the work of @Forsakn below. If you want to give credit, please scroll down and click "Like" on his post.

Lua:
-- TriggerRegisterVariableEvent hook to convert these old school events into something more useful.

do
    local events = {}
    local eventCount = 0

    function onRegisterVar(func)
        eventCount = eventCount + 1
        events[eventCount] = func
    end

    local oldEvent = TriggerRegisterVariableEvent
    function TriggerRegisterVariableEvent(trig, var, op, val)
        for i = 1, eventCount  do
            if events[i](trig, var, val) then return end
        end
        oldEvent(trig, var, op, val)
    end
end
 
Last edited:
Level 4
Joined
Mar 25, 2021
Messages
18
Hi, did the same thing I described here for this snippet to avoid desyncs.

Lua:
-- TriggerRegisterVariableEvent hook to convert these old school events into something more useful.

do
    local PRINT_CAUGHT_ERRORS = true
    local errors
    local errorCount = 0

    local events = {}
    local eventCount = 0
    local failedEvents = {}

    local function handleError(error)
        if PRINT_CAUGHT_ERRORS then
            if not errors then
                errors = {}

                TimerStart(
                    CreateTimer(),
                    1.00,
                    false,
                    function()
                        DestroyTimer(GetExpiredTimer())
                        print("|cffff0000TriggerRegisterVariableEvent: " .. #errors .. " errors occured during Initialization.|r")
                        for i = 1, errorCount do
                            print(errors[i])
                        end
                    end
                )
            end

            errorCount = errorCount + 1
            errors[errorCount] = "TriggerRegisterVariableEvent: caught error: " .. error
        end
    end

    function onRegisterVar(func)
        eventCount = eventCount + 1
        events[eventCount] = func
    end

    local oldEvent = TriggerRegisterVariableEvent
    function TriggerRegisterVariableEvent(trig, var, op, val)
        local registerOldEvent = true

        for i = 1, eventCount do
            if not failedEvents[i] then
                local result, data = pcall(events[i], trig, var, val)

                if not result then
                    failedEvents[i] = true
                    handleError(data)
                elseif data == true then registerOldEvent = false end
            end
        end

        if registerOldEvent then
            oldEvent(trig, var, op, val)
        end
    end
end

edit: Added use of pcall() and error printing to this as well, it also won't try to run failed calls multiple times.

Errors can be tested adding this script to your map, given you don't have functions with these names :grin:
Lua:
onRegisterVar(function(a, b, c) e() end)
onRegisterVar(function(a, b, c) f() end)

@Bribe I'm not sure about how the "if event() return end" was intended to work, was it just to skip old event registration firing if there exists an onRegisterVar event returning true? I modified it a bit, could you check if this still works as intended? without the modification onRegisterVar() calls with return value of true would skip registration of remaining events.
 
Last edited:
Top