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

[Trigger] Problem with counting units in unit group

Level 2
Joined
Apr 19, 2023
Messages
11
Hey there!

I was creating a spell based on (Attribute Bonus) that increases strength based on the number of allies around the hero. Im having some problems on getting the number of units in unit group, any help is appreciated.
  • Commander Copy
    • Events
      • Unit - A unit Learns a skill
    • Conditions
      • (Learned Hero Skill) Equal to Commander (Strength)
    • Actions
      • Set VariableSet Commander_TU = (Learning Hero)
      • Trigger - Turn on Commander Loop <gen>
  • Commander Loop
    • Events
      • Time - Every 0.50 seconds of game time
    • Conditions
      • (Level of Commander (Strength) for Commander_TU) Greater than 0
      • (Commander_TU is alive) Equal to True
    • Actions
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units within 800.00 of (Position of Commander_TU) matching ((((Matching unit) belongs to an enemy of (Owner of Commander_TU).) Equal to False) and ((((Matching unit) is A structure) Equal to False) and (((Matching unit) is dead) Equal to False))).) and do (Actions)
        • Loop - Actions
          • Set VariableSet Commander_NumberofUnits = (10 x (Number of units in (Last created unit group)))
          • Game - Display to (All players) the text: (Parse (String(Commander_NumberofUnits)))
          • Ability - Set Ability: (Unit: Commander_TU's Ability with Ability Code: Commander (Strength) )'s Integer Level Field: Strength Bonus ('Istr') of Level: ((Level of Commander (Strength) for Commander_TU) - 1) to Commander_NumberofUnits
          • Ability - Set Extended Tooltip of Commander (Strength) to for level ((Level of Commander (Strength) for Commander_TU) - 1)
          • Unit Group - Remove (Picked unit) from (Last created unit group).
 
Level 25
Joined
Sep 26, 2009
Messages
2,378
You leak a position (Position of Commander)
You include the commander himself in the unit group which I don't think is intended.
Instead of iterating the unit group and for each unit in the unit group perform the bonus strength calculation and update commander's ability, you should assign the unit group into a variable, just get the count of units in it and update commander's ability only once.

The final issue is that while the trigger action 'Ability - Set Ability: abil's Integer Level Field' does change the ability's data, for passive abilities you need to force the game to re-load the updated values. This does not always work (I think Evasion just cannot be changed) but in this case it works by just increasing and then decreasing the level of the ability (even if the ability has a single level).

So something like this should work:
  • periodic trigger
    • Events
      • Time - Every 0.50 seconds of game time
    • Conditions
    • Actions
      • Set VariableSet loc = (Position of hero)
      • Set VariableSet unitGroup = (Units within 800.00 of loc matching ...)
      • Set VariableSet unitCount = (Number of units in unitGroup)
      • Set VariableSet abilLevel = ((Level of CommanderAbil for hero) - 1)
      • Ability - Set Ability: (Unit: hero's Ability with Ability Code: CommanderAbil)'s Integer Level Field: Strength Bonus ('Istr') of Level: abilLevel to (10 x unitCount)
      • Unit - Increase level of CommanderAbil for hero
      • Unit - Decrease level of CommanderAbil for hero
      • Custom script: call RemoveLocation(udg_loc)
      • Custom script: call DestroyGroup(udg_unitGroup)
 
Level 2
Joined
Apr 19, 2023
Messages
11
You leak a position (Position of Commander)
You include the commander himself in the unit group which I don't think is intended.
Instead of iterating the unit group and for each unit in the unit group perform the bonus strength calculation and update commander's ability, you should assign the unit group into a variable, just get the count of units in it and update commander's ability only once.

The final issue is that while the trigger action 'Ability - Set Ability: abil's Integer Level Field' does change the ability's data, for passive abilities you need to force the game to re-load the updated values. This does not always work (I think Evasion just cannot be changed) but in this case it works by just increasing and then decreasing the level of the ability (even if the ability has a single level).

So something like this should work:
  • periodic trigger
    • Events
      • Time - Every 0.50 seconds of game time
    • Conditions
    • Actions
      • Set VariableSet loc = (Position of hero)
      • Set VariableSet unitGroup = (Units within 800.00 of loc matching ...)
      • Set VariableSet unitCount = (Number of units in unitGroup)
      • Set VariableSet abilLevel = ((Level of CommanderAbil for hero) - 1)
      • Ability - Set Ability: (Unit: hero's Ability with Ability Code: CommanderAbil)'s Integer Level Field: Strength Bonus ('Istr') of Level: abilLevel to (10 x unitCount)
      • Unit - Increase level of CommanderAbil for hero
      • Unit - Decrease level of CommanderAbil for hero
      • Custom script: call RemoveLocation(udg_loc)
      • Custom script: call DestroyGroup(udg_unitGroup)
Thanks! I didnt know locations leaked even outside of variables, gotta look back on some triggers then
 
Top