• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Running Method on Event occurs

Status
Not open for further replies.
Level 26
Joined
Aug 18, 2009
Messages
4,097
An event runs a trigger. The trigger registers functions to run via TriggerAddCondition or TriggerAddAction.

When the function is inside the same struct, write

call TriggerAddCondition(trigger, Condition(function thistype.method))

respectively

call TriggerAddAction(trigger, function thistype.method)

From outside of the struct, use

call TriggerAddCondition(trigger, Condition(function struct.method))

call TriggerAddAction(trigger, function struct.method)

For this, the method must be public (it is on default).
 
Level 9
Joined
Apr 23, 2011
Messages
460
They are. See code below.

JASS:
struct Jump extends array
    private static integer spell = 'Aroc'
    private static rect area = gg_rct_GiveSpell
    
    private static method GiveSpell takes nothing returns nothing
        local unit u = GetEnumUnit()
        local location loc = Location(GetRectCenterX(gg_rct_ResendSpell),GetRectCenterY(gg_rct_ResendSpell))
        call UnitAddAbility(u, spell)
        call SetUnitPositionLoc(u, loc)
        set loc = null
        set u = null
    endmethod
    
    private static method CheckUnit takes nothing returns nothing
        local group g = CreateGroup()
        call GroupEnumUnitsInRect(g, area, null)
        call ForGroup(g, function thistype.GiveSpell)
        call DestroyGroup(g)
        set g = null
    endmethod
    
    static method Main takes nothing returns nothing
        call .CheckUnit()
    endmethod
endstruct

They are taking and returning nothing. Should I not use static method main to call the private method CheckUnit and then just call CheckUnit in the trigger?
 
When is the static method "Main" being called? Try doing this instead:
JASS:
struct Jump extends array
    private static integer spell = 'Aroc'
    private static rect area = null
    
    private static method GiveSpell takes nothing returns nothing
        local unit u = GetEnumUnit()
        local location loc = Location(GetRectCenterX(gg_rct_ResendSpell),GetRectCenterY(gg_rct_ResendSpell))
        call UnitAddAbility(u, spell)
        call SetUnitPositionLoc(u, loc)
        set loc = null
        set u = null
    endmethod
    
    private static method CheckUnit takes nothing returns nothing
        local group g = CreateGroup()
        call DestroyTimer(GetExpiredTimer())
        set thistype.area = gg_rct_GiveSpell
        call GroupEnumUnitsInRect(g, area, null)
        call ForGroup(g, function thistype.GiveSpell)
        call DestroyGroup(g)
        set g = null
    endmethod
    
    private static method onInit takes nothing returns nothing
        call TimerStart(CreateTimer(), 0, false, function thistype.CheckUnit)
        // by running a 0 second timer you ensure that the region has been
        // created... otherwise it may return null as DSG said
    endmethod
endstruct
 
Level 9
Joined
Apr 23, 2011
Messages
460
Sorry, I use a jass trigger to compare multiple things and do more functions to call Main. I'll post this code now.
JASS:
function CheckUnitCond takes unit u returns boolean
    local integer typeUnit = 'hfoo'
    local region r = CreateRegion()
    call RegionAddRect(r, gg_rct_GiveSpell)
    if GetUnitTypeId(u) == typeUnit and IsUnitInRegion(r, u) == true then
        return true
    else
        return false
    endif
endfunction

function DoSpellGive takes nothing returns nothing
local unit u = GetEnumUnit()
   if CheckUnitCond(u) == true then
        call Jump.Main()
    endif
    set u = null
endfunction

function Main takes nothing returns nothing
    local group g = CreateGroup()
    call GroupEnumUnitsInRect(g, gg_rct_GiveSpell, null)
    call ForGroup(g, function DoSpellGive)
    call DestroyGroup(g)
    set g = null
endfunction
function InitTrig_Run_Jump takes nothing returns nothing
    local trigger t = CreateTrigger()
    call TriggerRegisterTimerEvent(t, 1., true)
    call TriggerAddAction(t, function Main)
    set t = null
endfunction
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
If my workaround didnt work, than you did something wrong

First, it would be GetFilterUnit() not GetEnumUnit()
you would also have to do returns boolean and return false at the end

in order to use the filter as the function

also, at your function, wtf is Jump.main()? if that was inside the script, it would just enum the units again causing a loop crash
 
Level 9
Joined
Apr 23, 2011
Messages
460
Jump.Main() is used by ForGroup(). ForGroup() sends a EnumUnit to the function called. From this function I run the checks on the unit, then if it returns true, i run Jump.Main() for that unit which gives it the command to train the unit to have an ability and move it through the struct.
 
Status
Not open for further replies.
Top