View Single Post
Old 09-21-2011, 11:50 AM   #7
BBQ
User
 
Join Date: May 2011
Posts: 85

Submissions (2)

BBQ will become famous soon enough (30)BBQ will become famous soon enough (30)

Default

Quote:
Originally Posted by Yrth
could you please show me what you mean?
not like something elaborate, just a short example... with a comment or two
Instead of doing this:
Collapse JASS:
globals
    group someGroup = CreateGroup()
endglobals

function unitCheck takes nothing returns boolean
    return (not IsUnitType(GetFilterUnit(), UNIT_TYPE_DEAD) and GetUnitTypeId(GetFilterUnit) == 'hfoo') // Enumerate only living footmen.
endfunction

function healthDisplay takes nothing returns nothing
    call BJDebugMsg(R2S(GetWidgetLife(GetEnumUnit())) // Display the health of all enumerated units (footmen).
endfunction

// Pretend that the following code is somewhere within a function.
call GroupEnumUnitsInRange(someGroup, 0.0, 0.0, 500.0, Filter(function unitCheck))
call ForGroup(someGroup, function healthDisplay)
call GroupClear(someGroup)

you should do this:
Collapse JASS:
globals
    unit filterUnit = null // We'll use this only to improve the efficiency.
    group someGroup = CreateGroup()
endglobals

function unitCheckAndHealthDisplay takes nothing returns boolean
    set filterUnit = GetFilterUnit() // We can't use a local, 'cos we won't get the chance to null it and prevent it from leaking.
    if (not IsUnitType(filterUnit, UNIT_TYPE_DEAD) and GetUnitTypeId(filterUnit) == 'hfoo') then // Check if the unit is a footman.
        call BJDebugMsg(R2S(GetWidgetLife(filterUnit)) // Display its health.
    endif
    return false // This prevents any unit from passing the filter, so not a single units gets added to the group. 
endfunction      // But there's no need to - as we've already done all actions we needed.

// Pretend that the following code is somewhere within a function.
call GroupEnumUnitsInRange(someGroup, 0.0, 0.0, 500.0, Filter(function unitCheckAndHealthDisplay))
// We no longer need to use ForGroup(), or even clear the group, because no units get added to it anyway.

Quote:
Originally Posted by Yrth
so to attach a struct I literally just do
Collapse JASS:
call SetTimerData(t, myStruct)

and then to call a method for that struct all I'd need to do is
Collapse JASS:
call GetTimerData(t).methodName
Yes, just don't forget to store the value of GetTimerData(someTimer) inside of a variable if you're using it multiple times.

Quote:
Originally Posted by Yrth
I'm guessing to do this I'd enumerate all the units into the group and then do a loop calling the function for the first of group and then removing the first from the group.
Could I do:
Collapse JASS:
exitwhen FirstOfGroup(g) == null

or would I have to use:
Collapse JASS:
exitwhen IsUnitGroupEmpty(g)
Here's what you should do:
Collapse JASS:
// Pretend that the following code is somewhere within a function.
local unit temporaryUnit

// Enumerate units and everything else...

loop
    set temporaryUnit = FirstOfGroup(someGroup) // Pretend that the group is filled with units.
    exitwhen (temporaryUnit == null)
    // Do stuff with the "temporaryUnit" unit.
endloop
BBQ is offline   Reply With Quote