[Trigger] How to work around leak? "Pick every unit of type"

Status
Not open for further replies.
Level 5
Joined
Aug 18, 2013
Messages
85
  • ManpowerFix
    • Events
      • Time - Every 60.00 seconds of game time
    • Conditions
    • Actions
      • Unit Group - Pick every unit in (Units of type House) and do (Player - Add 5 to (Owner of (Picked unit)) Current lumber)
      • Unit Group - Pick every unit in (Units of type Apartment Building) and do (Player - Add 10 to (Owner of (Picked unit)) Current lumber)

  • GrupCrash
    • Events
      • Time - Every 1.00 seconds of game time
    • Conditions
    • Actions
      • Unit Group - Pick every unit in Planes and do (If ((Mana of (Picked unit)) Less than 1.00) then do (Unit - Kill (Picked unit)) else do (Do nothing))
I use this trigger often and I heard making it a group variable and destroying it doesn't help either. Is there another way?
 
If you pick all units and then filter the unit type via If/Then/Else it's okay.
But better approach would be if you use more triggers to explicitly detect when a "income structure"
is build (then you increase income), and when a it's destroyed (then you decrease income).

Income would be an array variable then.
For example Income[1] is for player 1, Income[2] for player 2, ... etc.

In periodic trigger you would just loop through all players and give matching income with the Income[] variable.
 
Level 5
Joined
Aug 18, 2013
Messages
85
  • Unit Group - Pick every unit in (Units in (Playable map area)) and do (If ((Unit-type of (Matching unit)) Equal to Farm) then do (Player - Add 5 to (Owner of (Matching unit)) Current gold) else do (Do nothing))
Like this?
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
like this:
JASS:
globals
    group       ReturnGroup     = null
endglobals

function GetUnitsOfType takes integer unitid returns group
    local group   g      = CreateGroup()
    local unit FoG
    local integer index = 0
    set ReturnGroup = CreateGroup()
    
    loop
        call GroupEnumUnitsOfPlayer(g, Player(index), null)
        loop
            set FoG = FirstOfGroup(g)
            exitwhen FoG == null
            call GroupRemoveUnit(g, FoG)
            
            if GetUnitTypeId(FoG) == unitid then
                call GroupAddUnit(ReturnGroup, FoG)
            endif
        endloop
        set index = index + 1
        exitwhen index == bj_MAX_PLAYER_SLOTS
    endloop
    
    call DestroyGroup(g)
    set g = null
    return ReturnGroup
endfunction

Place that in the header file of your map. (header file is the WE iconed, map title named, text file at the top of the trigger list.)

Then use this to place the units of unit type "TempUnitType" in the group "TempGroup":
(create these two variables in the variable creator)
  • Actions
    • Set TempUnitType = Footman
    • Custom script: set udg_TempGroup = GetUnitsOfType(udg_TempUnitType)
    • Unit Group - Pick every unit in TempGroup and do (Actions)
      • Loop - Actions
        • -------- dosomething --------
    • Custom script: call DestroyGroup(udg_TempGroup)
NOTE:
Because of the global tags, you have to use JNGP to make it work.
And you will also have to save the map each time before you test it.

JASS:
function GetUnitsOfType takes integer unitid returns group
    local group   g      = CreateGroup()
    local unit FoG
    local integer index = 0
    set udg_ReturnGroup = CreateGroup()
    
    loop
        call GroupEnumUnitsOfPlayer(g, Player(index), null)
        loop
            set FoG = FirstOfGroup(g)
            exitwhen FoG == null
            call GroupRemoveUnit(g, FoG)
            
            if GetUnitTypeId(FoG) == unitid then
                call GroupAddUnit(udg_ReturnGroup, FoG)
            endif
        endloop
        set index = index + 1
        exitwhen index == bj_MAX_PLAYER_SLOTS
    endloop
    
    call DestroyGroup(g)
    set g = null
    return udg_ReturnGroup
endfunction
And you have to create the unit group variable "ReturnGroup" in the variable creator.
 
  • Unit Group - Pick every unit in (Units in (Playable map area)) and do (If ((Unit-type of (Matching unit)) Equal to Farm) then do (Player - Add 5 to (Owner of (Matching unit)) Current gold) else do (Do nothing))
Like this?
Yes, but use "Picked Unit", not "Matching Unit".
Also I would use ".. and Do Multiple Actions" always, because it is much more readable.
 
Status
Not open for further replies.
Top