• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Help with Unit Group please.

Status
Not open for further replies.
Level 6
Joined
Jun 20, 2005
Messages
108
Hello there.

Could you guys help me here? My function is making w3 crash, and I'm clueless about it. I'm just learning JASS, so I assume it's a rookie mistake.

Here's the code (well, part of it):

JASS:
    local unit L_CASTER = GetTriggerUnit()
    local player L_PLAYER = GetOwningPlayer(L_CASTER)
    local unit L_UNIT = GroupPickRandomUnit(GetUnitsInRectMatching(GetPlayableMapRect(), GetBooleanAnd(((GetUnitTypeId(GetFilterUnit()) == 'h000') == true), ( IsUnitEnemy(GetFilterUnit(), L_PLAYER) == true ))))

Thanks in advance.
 
Level 20
Joined
Apr 22, 2007
Messages
1,960
Here are some pointers:
1. Don't create a local variable unless you really need to, or unless you have to call a same function twice.
2. GetFilterUnit() only works in the GroupEnum filter functions (GetFilterUnit() is the equivalent of 'Matching Unit').
3. local variables generally (by convention) have lowercase names, but that's your call ;)
4. Try to avoid 'BJ' function calls. BJ's are functions found inside the blizzard.j Jass library. Usually BJ's are slow and stupid, so try to avoid them. There are of course exceptions, like the PolledWait function. Anyway...

Here's your code, rewritten and explained:
JASS:
function YourFilter takes nothing returns boolean
    return GetUnitTypeId(GetFilterUnit())=='h000' and IsUnitEnemy(GetFilterUnit(),bj_groupEnumOwningPlayer)
    // This is the filter function. We're returning a boolean of the condition.
endfunction
function Test takes nothing returns nothing
    local group g=CreateGroup() // We must create the group before any other actions on it.
    
    local unit L_UNIT // Here we declare the L_UNIT local.
    
    set bj_groupEnumOwningPlayer=GetOwningPlayer(GetTriggerUnit()) // We use a bj_ global to carry the player to the filter function.
    // bj_ globals aren't bad. In fact, they're good. You can find tons of them in blizzard.j
    
    call GroupEnumUnitsInRect(g,bj_mapInitialPlayableArea,Filter(function YourFilter)) // Here we get the units matching our filter.
    
    set L_UNIT=GroupPickRandomUnit(g) // We set L_UNIT as a random unit from the group
    // GroupPickRandomUnit is an O.K BJ...
    
    call DestroyGroup(g) // Cleaning memory leaks...
    set g=null // We always null local handles. Handles are every type in wc3 except integer, real, string, code, boolean
    set L_UNIT=null // Nulling another local
endfunction
 
Status
Not open for further replies.
Top