HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

2 quick questions

02-19-2007, 10:53 PM#1
Joker
1. How do i count the number of units in a unit group? i cant find the native for it.

2. How do i use the CasterCast natives that i see in jasscraft? Are they worth using?
02-19-2007, 10:59 PM#2
Alevice
2. user the caster system by vex. they are not natives though.
02-19-2007, 11:46 PM#3
Naakaloh
There is no native for it; there is a CountUnitsInGroup bj function.
02-20-2007, 03:49 AM#4
Pyrogasm
You could do it like this:
Collapse JASS:
function CountUnitsInGroup takes group G returns integer
    local integer I = 0
    local unit U
    loop
        set U = FirstOfGroup(G)
        exitwhen U == null
        set I = I+1
        call GroupRemoveUnit(G, U)
    endloop
    return I
endfunction
02-20-2007, 09:02 AM#5
zen87
Quote:
Originally Posted by Pyrogasm
You could do it like this:
Collapse JASS:
function CountUnitsInGroup takes group G returns integer
    local integer I = 0
    local unit U
    loop
        set U = FirstOfGroup(G)
        exitwhen U == null
        set I = I+1
        call GroupRemoveUnit(G, U)
    endloop
    return I
endfunction
that will bring to a side effect -.- all the unit will be removed after the counting, i'll suggest to do a copy group and so...

for those CasterCast natives, try search for vex's caster system for more information http://wc3campaigns.net/forumdisplay.php?f=588
02-20-2007, 10:07 AM#6
Pyrogasm
Quote:
Originally Posted by zen87
that will bring to a side effect -.- all the unit will be removed after the counting, i'll suggest to do a copy group and so...
Ah, right. Here's the correct way to do it:
Collapse JASS:
function CountUnitsInGroup takes group G returns integer
    local integer I = 0
    local unit U
    local group G2 = G
    loop
        set U = FirstOfGroup(G2)
        exitwhen U == null
        set I = I+1
        call GroupRemoveUnit(G2, U)
    endloop
    return I
endfunction
02-20-2007, 01:01 PM#7
shadow1500
I don't see why would you want to create a new function for it when the BJ works just fine and does it a lot faster. You have an error in your function:
Collapse JASS:
local group G2 = G
This statement does not clone the group, it sets the reference of G2 to G which would cause your code below to corrupt the original. In order to use this iteration method, you must clone the group by using GroupAddGroup to add the original into a newly created group and then iterate through that one. Also, the clone group must be destroyed and nullified, which you didn't do at the end of your function.
02-20-2007, 08:42 PM#8
Pyrogasm
Damn. I concede; I've failed.
02-20-2007, 09:42 PM#9
Rising_Dusk
Collapse JASS:
function LawlICanCount takes group g returns integer
    set bj_groupCountUnits = 0
    call ForGroup(g, function CountUnitsInGroupEnum)
    return bj_groupCountUnits
endfunction
I like this way better. :P