Tutorial - Convenient Unit Group Filtering in GUI

[center][color=#D0F680][SIZE=6][B]Convenient Unit Group Filtering in GUI[/B][/SIZE][/color]

[table][stable][goto=1]Introduction[/goto][r]
[goto=2]Common Structure[/goto][r]
[goto=3]Recomended Structure[/goto][r]
[goto=4]Conclusion[/goto][/stable]
[/table][/center]



[point]1[/point][h2][color=#D0F680]Introduction[/color][/h2]
[INDENT]This short tutorial will show you the best and most efficient way to filter out units enumerated from a unit group function. It will [U]not[/U] show you what usage the function offers (excluding the example trigger), nor how to use them. With that in mind, I recommend that you have moderate knowledge and experience with the trigger editor before reading on.[/INDENT]

[INDENT]The unit group function is probably one of the most useful functions in the Trigger Editor. A handful of the submitted spells and systems would simply not exist without it. Being an extremely common function, I can guarantee that any trigger that involves the manipulation of multiple units will use it.[/INDENT]

[INDENT][B]NOTE[/B]: The example triggers you will see throughout this tutorial will all achieve to do the same thing: deal 100 damage to enemy units, that are alive, within 300 range of the triggering unit.[/INDENT]



[point]2[/point][h2][color=#D0F680]Common Structure[/color][/h2]
[INDENT]As someone who lurks the [URL='https://www.hiveworkshop.com/forums/triggers-scripts-269/']Triggers & Scripts[/URL] and [URL='https://www.hiveworkshop.com/forums/world-editor-help-zone-98/']World Editor Help Zone[/URL] forums, I see a lot of people filtering units for a unit group like this:

[stable][trigger]Set Caster = (Triggering unit)
Set Player = (Triggering player)
Set CasterLoc = (Position of Caster)
-------- --------
Set UnitGroup = (Units within 300.00 of CasterLoc matching ((((Matching unit) is alive) Equal to True) and (((Matching unit) belongs to an enemy of Player) Equal to True)))
Unit Group - Pick every unit in UnitGroup and do (Actions)
Loop - Actions
Set TempUnit = (Picked unit)
Unit - Cause Caster to damage TempUnit, dealing 100.00 damage of attack type Spells and damage type Normal
Custom script: call DestroyGroup(udg_UnitGroup)
Custom script: call RemoveLocation(udg_CasterLoc)
[/trigger][r]
[trigger]Set Caster = (Triggering unit)
Set Player = (Triggering player)
Set CasterLoc = (Position of Caster)
-------- --------
Custom script: set bj_wantDestroyGroup = true
Unit Group - Pick every unit in (Units within 300.00 of CasterLoc matching ((((Matching unit) is alive) Equal to True) and (((Matching unit) belongs to an enemy of Player) Equal to True))) and do (Actions)
Loop - Actions
Set TempUnit = (Picked unit)
Unit - Cause Caster to damage TempUnit, dealing 100.00 damage of attack type Spells and damage type Normal
Custom script: call RemoveLocation(udg_CasterLoc)[/trigger][/stable][/INDENT]

[INDENT]Both will achieve the same thing, as explained in the introduction. All though there is nothing entirely wrong with it, there are a few issues that I
personally have with it:
[LIST]
[*](Matching unit) will constantly call the function GetFilterUnit(), which means that the execution will be slower.
[/LIST]
[LIST]
[*]They can get [U]excessively[/U] long when you have multiple filters (ex: magic immune, not a structure), making it a hassle to read and when copy-pasted as text, it will actually cut off at a certain point:
[hidden=Beware: ugly unit group filtering ahead][trigger]
Set UnitGroup = (Units within 300.00 of CasterLoc matching (((((Matching unit) is A structure) Equal to False) and (((Matching unit) is Magic Immune) Equal to False)) and ((((Matching unit) is alive) Equal to True) and (((Matching unit) belongs to an enemy of Player) Equal t
[/trigger][/hidden]
[/LIST]
[LIST]
[*]It is a complete pain in the a** to add, change, and or remove filters:
[hidden=Cringe worthy GIF][IMG]https://www.hiveworkshop.com/attachments/cringeunitgroupfiltering-gif.242113/[/IMG][/hidden]
[/LIST][/INDENT]
[point]3[/point][h2][color=#D0F680]Recommended Structure[/color][/h2]
[INDENT]If that GIF traumatized you from ever using unit groups again, fret not, for now I will show a much better way of doing it. Instead of using the (Matching unit) function, use an If/Then/Else, Multiple Actions function that directly compares the (Picked unit) to a filter:

[stable][trigger]Set Caster = (Triggering unit)
Set Player = (Triggering player)
Set CasterLoc = (Position of Caster)
-------- --------
Set UnitGroup = (Units within 300.00 of CasterLoc)
Unit Group - Pick every unit in UnitGroup and do (Actions)
Loop - Actions
Set TempUnit = (Picked unit)
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
(TempUnit is alive) Equal to True
(TempUnit belongs to an enemy of Player) Equal to True
Then - Actions
Unit - Cause Caster to damage TempUnit, dealing 100.00 damage of attack type Spells and damage type Normal
Else - Actions
Custom script: call DestroyGroup(udg_UnitGroup)
Custom script: call RemoveLocation(udg_CasterLoc)
[/trigger][r]
[trigger]Set Caster = (Triggering unit)
Set Player = (Triggering player)
Set CasterLoc = (Position of Caster)
-------- --------
Custom script: set bj_wantDestroyGroup = true
Unit Group - Pick every unit in (Units within 300.00 of CasterLoc) and do (Actions)
Loop - Actions
Set TempUnit = (Picked unit)
If (All Conditions are True) then do (Then Actions) else do (Else Actions)
If - Conditions
(TempUnit is alive) Equal to True
(TempUnit belongs to an enemy of Player) Equal to True
Then - Actions
Unit - Cause Caster to damage TempUnit, dealing 100.00 damage of attack type Spells and damage type Normal
Else - Actions
Custom script: call RemoveLocation(udg_CasterLoc)
[/trigger][/stable][/INDENT]



[point]4[/point][h2][color=#D0F680]Conclusion[/color][/h2]
[INDENT]Voila! Your unit group loop is now efficient, extremely easy to read, and requires little to no effort when you want to add and or remove filters. You also get the added benefit of me not scolding you in the Triggers & Scripts or Spell Section!
[/INDENT]

Attachments

  • CringeUnitGroupFiltering.gif
    CringeUnitGroupFiltering.gif
    897.6 KB · Views: 697
Last edited:
Back
Top