• 🏆 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!

[Solved] Help with removing a unit from unit group.

Status
Not open for further replies.
Level 20
Joined
May 16, 2012
Messages
635
So, i have an item trigger that increases hero stats and hp regen for each unit it kills, and it should stop increasing stats and stop regenerate hp when my unit do not have the item anymore. My initial idea was to check if my unit is in a global group and then, add that unit to that group if its not in it already. Using another trigger that fires when a unit loses an item i check if the unit has my specific item and if not, i remove it from my global group and that unit does not have hp regen anymore. The first part is working nicely, but the second part, where i remove the unit from the global group is not working, so what i should do?​

JASS:
function Elemental_Stone_Conditions takes nothing returns boolean
    return ( UnitHasItemOfTypeBJ(GetKillingUnit(), 'I039') == true )
endfunction

function Elemental_Stone_Hero_Filter takes nothing returns boolean
    return ( IsUnitType(GetDyingUnit(), UNIT_TYPE_HERO) == true )
endfunction

function Elemental_Stone_Actions takes nothing returns nothing
local unit u

    set u = GetKillingUnit()
    if ( Elemental_Stone_Hero_Filter() ) then
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10000 )
        call ModifyHeroStat( bj_HEROSTAT_STR, u, bj_MODIFYMETHOD_ADD, 5000 )
        call ModifyHeroStat( bj_HEROSTAT_AGI, u, bj_MODIFYMETHOD_ADD, 5000 )
        call ModifyHeroStat( bj_HEROSTAT_INT, u, bj_MODIFYMETHOD_ADD, 5000 )
    else
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10 )
        call ModifyHeroStat( bj_HEROSTAT_STR, u, bj_MODIFYMETHOD_ADD, 5 )
        call ModifyHeroStat( bj_HEROSTAT_AGI, u, bj_MODIFYMETHOD_ADD, 5 )
        call ModifyHeroStat( bj_HEROSTAT_INT, u, bj_MODIFYMETHOD_ADD, 5 )
    endif
    if(( IsUnitInGroup(u, udg_ES_Group) == false ))then
        call GroupAddUnit(udg_ES_Group, u)
    endif
    set u = null
endfunction

//===========================================================================
function InitTrig_Elemental_Stone takes nothing returns nothing
    set gg_trg_Elemental_Stone = CreateTrigger(  )
    call DisableTrigger(gg_trg_Elemental_Stone)
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Elemental_Stone, EVENT_PLAYER_UNIT_DEATH )
    call TriggerAddCondition( gg_trg_Elemental_Stone, Condition( function Elemental_Stone_Conditions ) )
    call TriggerAddAction( gg_trg_Elemental_Stone, function Elemental_Stone_Actions )
endfunction
This is the first part and is working perfectly.
JASS:
function Es_Removal_Conditions takes nothing returns boolean
    return ( GetManipulatedItem() == GetItemOfTypeFromUnitBJ(GetManipulatingUnit(), 'I039') )
endfunction

function Es_Removal_Actions takes nothing returns nothing
local item i
local unit u

    set u = GetManipulatingUnit()
    set i = GetItemOfTypeFromUnitBJ(u, 'I039')
    if(i == null)then
        call GroupRemoveUnit(udg_ES_Group, u)
    endif
    set u = null
    set i = null
endfunction

//===========================================================================
function InitTrig_Es_Removal takes nothing returns nothing
    set gg_trg_Es_Removal = CreateTrigger(  )
    call DisableTrigger(gg_trg_Es_Removal)
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Es_Removal, EVENT_PLAYER_UNIT_DROP_ITEM )
    call TriggerAddCondition( gg_trg_Es_Removal, Condition( function Es_Removal_Conditions ) )
    call TriggerAddAction( gg_trg_Es_Removal, function Es_Removal_Actions )
endfunction
And here is my problem. when my unit loses the item it stop gaining stats from future kills but the hp regeneration persist even when i remove my unit from the group (wtf?).

JASS:
function ES_Heal takes nothing returns nothing
local unit u

    set u = GetEnumUnit()
    call DestroyEffect(AddSpecialEffectTargetUnitBJ( "origin", u, "war3mapImported\\PurpleSphere.mdl" ))
    call SetUnitLifeBJ( u, ( GetUnitStateSwap(UNIT_STATE_LIFE, u) + I2R(udg_ES_Integer[GetUnitPointValue(u)]) ) )
    set u = null
endfunction

function Es_Effect_Actions takes nothing returns nothing
    call ForGroup(udg_ES_Group, function ES_Heal)
endfunction

//===========================================================================
function InitTrig_Es_Effect takes nothing returns nothing
    set gg_trg_Es_Effect = CreateTrigger(  )
    call DisableTrigger( gg_trg_Es_Effect )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Es_Effect, 0.5 )
    call TriggerAddAction( gg_trg_Es_Effect, function Es_Effect_Actions )
endfunction
This is the heal trigger, that takes every unit on it and heal by their previously specified value.
 
Last edited:
Level 8
Joined
Jan 28, 2016
Messages
486
And here is my problem. when my unit loses the item it stop gaining stats from future kills but the hp regeneration persist even when i remove my unit from the group (wtf?).

You should add the rest of the Jass code and functions to your OP (InitTrig, Conditions, etc.) just to get a clearer picture of what's going on. At the moment, I can't see why this would happen. Try adding some BJDebugMsg calls in the If-Then-Else statement to see what gets called. As for those stat functions:

JASS:
function Elemental_Stone_Actions takes nothing returns nothing
    local unit u = GetKillingUnit()
    if ( Elemental_Stone_Hero_Filter() ) then
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10000 )
        call SetHeroAgi( u, GetHeroAgi( u, false) + 5000, true )
        call SetHeroInt( u, GetHeroInt( u, false) + 5000, true )
        call SetHeroStr( u, GetHeroStr( u, false) + 5000, true )
    else
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10 )
        call SetHeroAgi( u, GetHeroAgi( u, false) + 5, true )
        call SetHeroInt( u, GetHeroInt( u, false) + 5, true )
        call SetHeroStr( u, GetHeroStr( u, false) + 5, true )
    endif
    if IsUnitInGroup( u, udg_ES_Group ) == false then
       call GroupAddUnit( udg_ES_Group, u )
    endif
    set u = null
endfunction
 
Level 20
Joined
May 16, 2012
Messages
635
You should add the rest of the Jass code and functions to your OP (InitTrig, Conditions, etc.) just to get a clearer picture of what's going on. At the moment, I can't see why this would happen. Try adding some BJDebugMsg calls in the If-Then-Else statement to see what gets called. As for those stat functions:

JASS:
function Elemental_Stone_Actions takes nothing returns nothing
    local unit u = GetKillingUnit()
    if ( Elemental_Stone_Hero_Filter() ) then
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10000 )
        call SetHeroAgi( u, GetHeroAgi( u, false) + 5000, true )
        call SetHeroInt( u, GetHeroInt( u, false) + 5000, true )
        call SetHeroStr( u, GetHeroStr( u, false) + 5000, true )
    else
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10 )
        call SetHeroAgi( u, GetHeroAgi( u, false) + 5, true )
        call SetHeroInt( u, GetHeroInt( u, false) + 5, true )
        call SetHeroStr( u, GetHeroStr( u, false) + 5, true )
    endif
    if IsUnitInGroup( u, udg_ES_Group ) == false then
       call GroupAddUnit( udg_ES_Group, u )
    endif
    set u = null
endfunction
Sorry for that, i've updated the thread. All functions related to the item are there.
 
Level 8
Joined
Jan 28, 2016
Messages
486
You have the 2nd and 3rd triggers disabled: call DisableTrigger( gg_trg_Es_Effect ).

Make sure that when you convert GUI triggers that they're enabled, otherwise the Editor adds that line. I know from personal experience! :p
 
Level 20
Joined
May 16, 2012
Messages
635
You have the 2nd and 3rd triggers disabled: call DisableTrigger( gg_trg_Es_Effect ).

Make sure that when you convert GUI triggers that they're enabled, otherwise the Editor adds that line. I know from personal experience! :p
I Enable them when some unit acquire the item, to avoid unnecessary triggers to be fired, so that is not the problem
 
Level 8
Joined
Jan 28, 2016
Messages
486
I realised what the problem was; your checking to see if the item is on the hero in the If-Then-Else statement of the 2nd trigger, even though it is run when you drop the item. The ITE will always turn out false and never remove the hero from the group. Instead, using the trigger condition to check which item is dropped, you can simply delete all the other actions and the ITE and call the GroupRemoveUnit function.

I've posted my working version of your code in the spoiler below. I added an extra event to the second trigger which detects when a hero picks up an item of that type and the adds it to the group, in turn getting rid of the second ITE in the first trigger (the one that adds the hero to the group after killing a unit; in retrospect that didn't make much sense). I haven't added any checks to disable any of the triggers but I guess you could add that in yourself. I also got a little carried away and replaced all the BJ functions except for the GetItemOfTypeFromUnitBJ function (I'm not familiar with it).

JASS:
function ES_Kill_Actions takes nothing returns nothing
    local unit u = GetKillingUnit()
    if IsUnitType(GetDyingUnit(), UNIT_TYPE_HERO) == true then
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10000 )
        call SetHeroAgi( u, GetHeroAgi( u, false) + 5000, true )
        call SetHeroInt( u, GetHeroInt( u, false) + 5000, true )
        call SetHeroStr( u, GetHeroStr( u, false) + 5000, true )
    else
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10 )
        call SetHeroAgi( u, GetHeroAgi( u, false) + 5, true )
        call SetHeroInt( u, GetHeroInt( u, false) + 5, true )
        call SetHeroStr( u, GetHeroStr( u, false) + 5, true )
    endif
    set u = null
endfunction

function ES_Kill_Conditions takes nothing returns boolean
    if IsUnitType(GetKillingUnit(), UNIT_TYPE_HERO) == true and UnitHasItemOfTypeBJ(GetKillingUnit(), 'I039') == true then
        call ES_Kill_Actions()
    endif
    return false
endfunction

//===========================================================================
function InitTrig_ES_Kill takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_DEATH )
    call TriggerAddCondition( t, Condition( function ES_Kill_Conditions ) )
    set t = null
endfunction

JASS:
function ES_AddRemove_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    if GetTriggerEventId() == EVENT_PLAYER_UNIT_PICKUP_ITEM then
        call GroupAddUnit( udg_ES_Group, u )
    elseif GetTriggerEventId() == EVENT_PLAYER_UNIT_DROP_ITEM then
        call GroupRemoveUnit( udg_ES_Group, u )
    endif
    set u = null
endfunction

function ES_AddRemove_Conditions takes nothing returns boolean
    if GetManipulatedItem() == GetItemOfTypeFromUnitBJ( GetTriggerUnit(), 'I039' ) then
        call ES_AddRemove_Actions()
    endif
    return false
endfunction

//===========================================================================
function InitTrig_ES_AddRemove takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_PICKUP_ITEM )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_DROP_ITEM )
    call TriggerAddCondition( t, Condition( function ES_AddRemove_Conditions ) )
    set t = null
endfunction

JASS:
function ES_Effect_Heal takes nothing returns nothing
    local unit u = GetEnumUnit()
    call DestroyEffect(AddSpecialEffectTarget( "Abilities\\Spells\\Human\\ThunderClap\\ThunderClapCaster.mdl", u, "origin" ))
    call SetUnitState( u, UNIT_STATE_LIFE, GetUnitState( u, UNIT_STATE_LIFE) + I2R(udg_ES_Integer[GetUnitPointValue(u)]) )
    set u = null
endfunction

function ES_Effect_Actions takes nothing returns nothing
    call ForGroup(udg_ES_Group, function ES_Effect_Heal)
endfunction

//===========================================================================
function InitTrig_ES_Effect takes nothing returns nothing
    set gg_trg_ES_Effect = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_ES_Effect, 0.5 )
    call TriggerAddAction( gg_trg_ES_Effect, function ES_Effect_Actions )
endfunction
 
Level 20
Joined
May 16, 2012
Messages
635
I realised what the problem was; your checking to see if the item is on the hero in the If-Then-Else statement of the 2nd trigger, even though it is run when you drop the item. The ITE will always turn out false and never remove the hero from the group. Instead, using the trigger condition to check which item is dropped, you can simply delete all the other actions and the ITE and call the GroupRemoveUnit function.

I've posted my working version of your code in the spoiler below. I added an extra event to the second trigger which detects when a hero picks up an item of that type and the adds it to the group, in turn getting rid of the second ITE in the first trigger (the one that adds the hero to the group after killing a unit; in retrospect that didn't make much sense). I haven't added any checks to disable any of the triggers but I guess you could add that in yourself. I also got a little carried away and replaced all the BJ functions except for the GetItemOfTypeFromUnitBJ function (I'm not familiar with it).

JASS:
function ES_Kill_Actions takes nothing returns nothing
    local unit u = GetKillingUnit()
    if IsUnitType(GetDyingUnit(), UNIT_TYPE_HERO) == true then
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10000 )
        call SetHeroAgi( u, GetHeroAgi( u, false) + 5000, true )
        call SetHeroInt( u, GetHeroInt( u, false) + 5000, true )
        call SetHeroStr( u, GetHeroStr( u, false) + 5000, true )
    else
        set udg_ES_Integer[GetUnitPointValue(u)] = ( udg_ES_Integer[GetUnitPointValue(u)] + 10 )
        call SetHeroAgi( u, GetHeroAgi( u, false) + 5, true )
        call SetHeroInt( u, GetHeroInt( u, false) + 5, true )
        call SetHeroStr( u, GetHeroStr( u, false) + 5, true )
    endif
    set u = null
endfunction

function ES_Kill_Conditions takes nothing returns boolean
    if IsUnitType(GetKillingUnit(), UNIT_TYPE_HERO) == true and UnitHasItemOfTypeBJ(GetKillingUnit(), 'I039') == true then
        call ES_Kill_Actions()
    endif
    return false
endfunction

//===========================================================================
function InitTrig_ES_Kill takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_DEATH )
    call TriggerAddCondition( t, Condition( function ES_Kill_Conditions ) )
    set t = null
endfunction

JASS:
function ES_AddRemove_Actions takes nothing returns nothing
    local unit u = GetTriggerUnit()
    if GetTriggerEventId() == EVENT_PLAYER_UNIT_PICKUP_ITEM then
        call GroupAddUnit( udg_ES_Group, u )
    elseif GetTriggerEventId() == EVENT_PLAYER_UNIT_DROP_ITEM then
        call GroupRemoveUnit( udg_ES_Group, u )
    endif
    set u = null
endfunction

function ES_AddRemove_Conditions takes nothing returns boolean
    if GetManipulatedItem() == GetItemOfTypeFromUnitBJ( GetTriggerUnit(), 'I039' ) then
        call ES_AddRemove_Actions()
    endif
    return false
endfunction

//===========================================================================
function InitTrig_ES_AddRemove takes nothing returns nothing
    local trigger t = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_PICKUP_ITEM )
    call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_DROP_ITEM )
    call TriggerAddCondition( t, Condition( function ES_AddRemove_Conditions ) )
    set t = null
endfunction

JASS:
function ES_Effect_Heal takes nothing returns nothing
    local unit u = GetEnumUnit()
    call DestroyEffect(AddSpecialEffectTarget( "Abilities\\Spells\\Human\\ThunderClap\\ThunderClapCaster.mdl", u, "origin" ))
    call SetUnitState( u, UNIT_STATE_LIFE, GetUnitState( u, UNIT_STATE_LIFE) + I2R(udg_ES_Integer[GetUnitPointValue(u)]) )
    set u = null
endfunction

function ES_Effect_Actions takes nothing returns nothing
    call ForGroup(udg_ES_Group, function ES_Effect_Heal)
endfunction

//===========================================================================
function InitTrig_ES_Effect takes nothing returns nothing
    set gg_trg_ES_Effect = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_ES_Effect, 0.5 )
    call TriggerAddAction( gg_trg_ES_Effect, function ES_Effect_Actions )
endfunction
You sir, deserve a cookie s2 +rep
 
Status
Not open for further replies.
Top