Warcraft III resources & community, 2003–2006 · archived

WaitForUnitEvent

not rated
Submitted by PepparExecution0 downloads
This function halts the calling trigger (thread) until the specified unit event occurs. It checks with the specified interval. An interval of 0 makes the function check the event as often as possible. The condition works like the one of a normal trigger. If you don't want to use a condition, pass null to the function.

You can pass either a unit or a unit group to the function. If you pass a unit group the function will wait until the specified event occurs to *any* of the units in the group. The condition can be used to control this however.

This function uses IterateGroup© technology, many thanks to dataangel. Inlined for optimization.

Example:

function myfunction_conditions takes nothing returns boolean
return GetTriggerPlayer() == GetOwningPlayer(GetTriggerUnit())
endfunction

local conditionfunc c = Condition(function myfunction_conditions)
call WaitForUnitEvent(udg_my_unit, EVENT_UNIT_SELECTED, 0, c)
call DestroyCondition(c)

In this example the function waits for udg_my_unit to be selected by its owner, checking as often as possible. BoolExprMakeTemporary doesn't work for destroying the condition as the function sleeps and requires the condition to be available until the function has finished, so you have to create/destroy it manually.

If null was used as condition to the function it would stop waiting when *any* player selected the unit.

If a unit group was used instead of udg_my_unit then the function would stop waiting when *any* of the units were selected by its owner.

Source

function WaitForUnitEvent_H2G takes handle h returns group
    return h
endfunction

function WaitForUnitEvent_H2U takes handle h returns unit
    return h
endfunction

function WaitForUnitEvent takes handle whichGroupOrUnit, unitevent whichUnitEvent, boolexpr condition, real interval returns nothing
    local trigger t
    local triggercondition c
    local group whichGroup = WaitForUnitEvent_H2G(whichGroupOrUnit)
    local unit whichUnit = WaitForUnitEvent_H2U(whichGroupOrUnit)
    local group g
    if whichGroupOrUnit == null then
        set whichUnit = null
        set whichGroup = null
        return
    elseif interval < 0 then
        set interval = 0
    endif
    set t = CreateTrigger()
    if FirstOfGroup(whichGroup) == null then
        call TriggerRegisterUnitEvent(t, whichUnit, whichUnitEvent)
    else
        set g = CreateGroup()
        call GroupAddGroup(whichGroup, g)
        loop
            set whichUnit = FirstOfGroup(g)
            exitwhen whichUnit == null
            call TriggerRegisterUnitEvent(t, whichUnit, whichUnitEvent)
            call GroupRemoveUnit(g, whichUnit)
        endloop
        call DestroyGroup(g)
        set g = null
    endif
    if condition != null then
        set c = TriggerAddCondition(t, condition)
    endif
    loop
        call TriggerSleepAction(interval)
        exitwhen GetTriggerExecCount(t) != 0
    endloop
    if condition != null then
        call TriggerRemoveCondition(t, c)
        set c = null
    endif
    call DestroyTrigger(t)
    set t = null
    set whichUnit = null
    set whichGroup = null
endfunction

Comments

0

No comments.