Anonymous functions?

Manix

Well-Known Member
Reaction score
29
Hey, I'm just getting into jass and I'm making a little spell effect in order to learn. But what is bugging me is that I can't seem to figure out how to pass anonymous functions, for example in the code I have

JASS:
scope AttackSFX initializer init

private function Cond takes nothing returns boolean
    if(GetUnitAbilityLevel(GetAttacker(),'AHbh')>0) then
        if( GetRandomInt(1,5) == 1 ) then
            return true
        endif
    endif
    return false
endfunction

private function DamageGroupCond takes nothing returns boolean
    return ( IsUnitEnemy(GetFilterUnit(), GetOwningPlayer(GetAttacker())) == true )
endfunction

private function DamageGroup takes nothing returns nothing
    call UnitDamageTarget( GetAttacker(), GetEnumUnit(), 200, true, false, ATTACK_TYPE_HERO, DAMAGE_TYPE_MAGIC, WEAPON_TYPE_AXE_MEDIUM_CHOP)
endfunction

private function Actions takes nothing returns nothing
    local location p
    local effect e
    local group ug
    set p = GetUnitLoc(GetAttacker())
    set e = AddSpecialEffectLoc("Abilities\\Spells\\Other\\HowlOfTerror\\HowlCaster.mdl" , p)
    set e = null
    set ug = GetUnitsInRangeOfLocMatching(300, p, function DamageGroupCond)
    call ForGroup(ug,function DamageGroup)
    set p = null
    set ug = null
endfunction

private function init takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_ATTACKED)
    call TriggerAddCondition(t, function Cond)
    call TriggerAddAction( t, function Actions )
    set t = null
endfunction

endscope


How could I pass the group actions and conditions as anonymous functions?
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
There aren't anonymous functions in JASS/vJASS. They do exist in Zinc, however:
http://www.wc3c.net/vexorian/zincmanual.html#anonfunc

But keep in mind that vJASS, Zinc, wurst, cJASS, etc. all compile into regular JASS, so they are bound by the same restrictions that regular JASS has. As far as groups go, the filter belongs in a separate function and the ForGroup() belongs in a separate function (or you can use one for both, e.g. run the actions within the filter, or just have an if-block in the ForGroup() and input null for the filter). If you need to pass data to the ForGroup()/filter callback, you can just use globals (don't worry, you won't run into MUI issues).

There is an alternative method, known as a FirstOfGroup() loop. It is used when you don't need to maintain the units in the group (e.g. you just need to perform actions to the group, and then you're done with it). If you need info, perhaps this post will be of use:
http://www.hiveworkshop.com/forums/2411951-post10.html
It should have all the info you need on how to use groups and when to use the different callback/iterative methods.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top