Dummy only cast spell at one target in ForGroup

Magoiche

Member
Reaction score
20
I made this trigger to when i cast a spell, a dummy is spawned and some other dummies will cast Life Drain on units near.
But only one dummy cast Life Drain.

JASS:
scope SuckingLight initializer Initial

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A002'
endfunction

private function TargetConditions takes nothing returns boolean
    return IsUnitEnemy( GetFilterUnit(), GetOwningPlayer( GetTriggerUnit() ) ) == true and GetUnitState( GetFilterUnit(), UNIT_STATE_LIFE ) > 0
endfunction

private function Light takes nothing returns nothing
    local unit Caster = GetTriggerUnit()
    local unit Dummy = CreateUnitAtLoc( GetOwningPlayer( Caster ), 'u002', GetSpellTargetLoc(), 0.0 )
    call UnitApplyTimedLife( Dummy, 'BTLF', 20.0 )
    call UnitAddAbility( Dummy, 'A003' )
    call SetUnitAbilityLevel( Dummy, 'A003', GetUnitAbilityLevel( Caster, 'A002' ) )
    call IssueTargetOrder( Dummy, "drain", GetEnumUnit() )
    set Caster = null
    set Dummy = null
endfunction

private function Actions takes nothing returns nothing
    local unit Caster = GetTriggerUnit()
    local unit Dummy = CreateUnitAtLoc( GetOwningPlayer( Caster ), 'u000', GetSpellTargetLoc(), 0.0 )
    local group UnitsNear = GetUnitsInRangeOfLocMatching( 800, GetUnitLoc( Dummy ), Condition( function TargetConditions ) )
    call SetUnitColor( Dummy, PLAYER_COLOR_YELLOW )
    if CountUnitsInGroup( UnitsNear ) == 0 then
        call KillUnit( Dummy )
    else
        call UnitApplyTimedLife( Dummy, 'BFTL', 20.0 )
        call ForGroup( UnitsNear, function Light )
    endif
    call DestroyGroup( UnitsNear )
    set Caster = null
    set Dummy = null
    set UnitsNear = null
endfunction

private function Initial takes nothing returns nothing
    local trigger trig = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( trig, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( trig, Condition( function Conditions ) )
    call TriggerAddAction( trig, function Actions )
    set trig = null
endfunction

endscope


I can't see where is the error. x.x

Quick Question

1. What GroupClear do? Empty the group? The same as DestroyGroup?

Thanks in advance. =]
 

Flare

Stops copies me!
Reaction score
662
(havne't looked at code, not sure of what the problem would be there :p)

What GroupClear do? Empty the group? The same as DestroyGroup?
GroupClear empties the group, but the group still exists
DestroyGroup (as the name suggests) destroys the group (and presumably empties, not sure what would happen if a new group took a destroyed group's recycled ID), so it's no longer usable unless you do CreateGroup again

GroupClear will not solve a group leak, but if you're using a single global group variable and doing
JASS:
call GroupEnumUnitsInRange (...)
//or
call GroupAddUnit (...)

you can just clear the group if/when the group needs to be emptied of all units, and you shouldn't have any bother.

If you're doing
JASS:
local group g = CreateGroup ()

you're gonna need to use
JASS:
call DestroyGroup (g)
 

chobibo

Level 1 Crypt Lord
Reaction score
48
EDIT: Oh my mistake... sorry.

EDIT2: Sorry dude, I can't also see any errors. Could you post the map? Oh you're also leaking locations, GetSpellTargetLoc() leaks a location. Try storing it to a variable and then RemoveLocation() it.
 

Magoiche

Member
Reaction score
20
@Flare
Thanks!
It will be usefull to me =]

@chobibo
Sorry for what? x.x

Attached the map on the first post.

Note: When i copied the map then opened to remove some things.
The jasshelper did not show up when i was saving. lol
So if you get any error its not my fault. u.u

Note2: Thanks. I had this leak in some other spells too. =]
 

chobibo

Level 1 Crypt Lord
Reaction score
48
JASS:
scope SuckingLight initializer InitScope

globals
    private constant integer    ABILITY_ID         =    'A002'
    private constant integer    DUMMY_DRAIN_ID     =    'A003'
    private constant integer    DUMMY_MASTER       =    'u000'
    private constant integer    DUMMY_CHILD        =    'u002'
    
    private constant real       AREA_OF_EFFECT     =    500.00
    
    private group TARGETS
endglobals

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == ABILITY_ID
endfunction

private function TargetConditions takes nothing returns boolean
    return IsUnitEnemy( GetFilterUnit(), GetTriggerPlayer() ) == true and GetUnitState( GetFilterUnit(), UNIT_STATE_LIFE ) > 0
endfunction

private function Actions takes nothing returns nothing
    local location loc = GetSpellTargetLoc()
    local integer level = GetUnitAbilityLevel( GetTriggerUnit(), ABILITY_ID )
    
    local unit dummy
    local unit target
    
    local boolean EmptyGroup
    
    call GroupEnumUnitsInRangeOfLoc(TARGETS, loc, AREA_OF_EFFECT, Condition(function TargetConditions))
    
    set EmptyGroup=(FirstOfGroup(TARGETS)==null)
    
    loop
        set target=FirstOfGroup(TARGETS)
        exitwhen target==null
        
        set dummy=CreateUnitAtLoc(GetTriggerPlayer(), DUMMY_CHILD, loc, 0)
        call ShowUnit(dummy, false)
        
        call UnitApplyTimedLife(dummy, 0, 20.0)
        
        call UnitAddAbility(dummy, DUMMY_DRAIN_ID)
        call SetUnitAbilityLevel(dummy, DUMMY_DRAIN_ID, level)
        call IssueTargetOrder(dummy, "drain", target)
        
        call GroupRemoveUnit(TARGETS, target)
    endloop
    
    if not (EmptyGroup) then
        set dummy=CreateUnitAtLoc(GetTriggerPlayer(), DUMMY_MASTER, loc, 0)
        call SetUnitColor(dummy, PLAYER_COLOR_YELLOW)
        call UnitApplyTimedLife(dummy, 0, 20.0)
    endif
    
    call GroupClear(TARGETS)
    call RemoveLocation(loc)
    
    set loc = null
    set dummy = null
    set target = null
endfunction

private function InitScope takes nothing returns nothing
    local trigger trig = CreateTrigger()
    
    call TriggerRegisterAnyUnitEventBJ( trig, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( trig, Condition( function Conditions ) )
    call TriggerAddAction( trig, function Actions )

    set TARGETS=CreateGroup()
endfunction

endscope


I don't know exactly why ForGroup didn't work, I didn't want to change anything in the Object Editor so I tried FirstOfGroup() and it turned out okay.

EDIT: Wrong conclusion.

EDIT 2: AH now I get it!, whenever the dummy caster casts a spell the GetSpellTargetLoc() is modified so it returns something else. I'll post the solution later.


Here's the quick fix to your spell. I haven't edited it, just added the fix, so it leaks and is not optimized.
JASS:
scope SuckingLight initializer Initial

    globals
       private location LOC
    endglobals

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A002'
endfunction

private function TargetConditions takes nothing returns boolean
    return IsUnitEnemy( GetFilterUnit(), GetOwningPlayer( GetTriggerUnit() ) ) == true and GetUnitState( GetFilterUnit(), UNIT_STATE_LIFE ) > 0
endfunction

private function Light takes nothing returns nothing
    local unit Caster = GetTriggerUnit()
    local unit Dummy = CreateUnitAtLoc( GetOwningPlayer( Caster ), 'u002', LOC, 0.0 )
    
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, GetUnitName(GetEnumUnit()))
    
    call UnitApplyTimedLife( Dummy, 'BTLF', 20.0 )
    call UnitAddAbility( Dummy, 'A003' )
    call SetUnitAbilityLevel( Dummy, 'A003', GetUnitAbilityLevel( Caster, 'A002' ) )
    call IssueTargetOrder( Dummy, "drain", GetEnumUnit() )
    
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "callback: "+I2S(H2I(LOC)))
    
    set Caster = null
    set Dummy = null
endfunction

private function Actions takes nothing returns nothing
    local unit Caster = GetTriggerUnit()
    local unit Dummy 
    local group UnitsNear
    
    set LOC=GetSpellTargetLoc()
    
    set Dummy=CreateUnitAtLoc( GetOwningPlayer( Caster ), 'u000', LOC, 0.0 )
    set UnitsNear=GetUnitsInRangeOfLocMatching( 800, GetUnitLoc( Dummy ), Condition( function TargetConditions ) )

    
    call SetUnitColor( Dummy, PLAYER_COLOR_YELLOW )
    
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "before callback: "+I2S(H2I(LOC)))

    
    if CountUnitsInGroup( UnitsNear ) == 0 then
        call KillUnit( Dummy )
    else
        call UnitApplyTimedLife( Dummy, 'BFTL', 20.0 )
        call ForGroup( UnitsNear, function Light )
    endif
    
    call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "after callback: "+I2S(H2I(LOC)))

    call RemoveLocation(LOC)
    
    call DestroyGroup( UnitsNear )
    set Caster = null
    set Dummy = null
    set UnitsNear = null
endfunction

private function Initial takes nothing returns nothing
    local trigger trig = CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ( trig, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( trig, Condition( function Conditions ) )
    call TriggerAddAction( trig, function Actions )
    set trig = null
endfunction

endscope
 

Magoiche

Member
Reaction score
20
Yeah. I figured it out too.
But... Making that location a global make the spell non-mui(not sure).
I think i will try changing somethings. i.i

Thanks anyway.
 

chobibo

Level 1 Crypt Lord
Reaction score
48
I already provided a new code without leaks (it's the first one), it's also MUI, if ever you need it you can use it.
 

Magoiche

Member
Reaction score
20
Cool!
You use FirstofGroup then started removing the already target units.
This is going to my remember book. =]

Very big thanks to you.
 
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