• 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 my JASS

Status
Not open for further replies.
Level 5
Joined
Aug 16, 2007
Messages
149
I just started learning JASS and I'm trying to experiment with some code, and I wrote some script and it doesn't seem to work properly but it looks fine to my amateur eyes. It is supposed to kill every Nth unit in a unit group(integer n is a parameter) and it calls another function I wrote which I included but I don't think that's the problem. here's my code:
JASS:
function GetNthOfGroup takes group g,integer i returns unit
    local group g2 = g
    local integer i2 = 0
    loop
        call GroupRemoveUnit(g,FirstOfGroup(g))
        set i2 = i2 + 1
        if( i2 == i )then
            set i2 = 0
        endif
        exitwhen i2==0
    endloop
    return FirstOfGroup(g2)
endfunction

function KillEveryNthUnitInGroup takes group g, integer n, boolean removeUnits returns nothing
    local group g2 = g
    local integer i = CountUnitsInGroup(g)/(n-1)
    local integer i2 = 0
    local integer i3 = 0
    set n = n-1
    loop
        exitwhen i2 > i
        call KillUnit( GetNthOfGroup( g2, n ) )
        call GroupRemoveUnit(g2, GetNthOfGroup(g2,n))
        if( removeUnits == true) then
            call GroupRemoveUnit( g, GetNthOfGroup(g2,n))
        endif
        loop
            exitwhen i3 == n
            call GroupRemoveUnit(g2,FirstOfGroup(g2))
            set i3 = i3 + 1
        endloop
        set i2 = i2 + 1     
    endloop    
endfunction
could someone help me improve my JASS and make it work properly? I really want to learn JASS!:wsad:
 
Level 5
Joined
Aug 16, 2007
Messages
149
ok... i think i understand that... but GroupAddGroup doesn't seem to work: it just does nothing when I try to call the function when before it at least killed some of them. heres the new code but it still doesn't work
JASS:
function GetNthOfGroup takes group g,integer i returns unit
    local group g2
    local integer i2 = 0
    call GroupAddGroup(g,g2)
    loop
        call GroupRemoveUnit(g,FirstOfGroup(g))
        set i2 = i2 + 1
        if( i2 == i )then
            set i2 = 0
        endif
        exitwhen i2==0
    endloop
    return FirstOfGroup(g2)
endfunction

function KillEveryNthUnitInGroup takes group g, integer n, boolean removeUnits returns nothing
    local group g2
    local integer i = CountUnitsInGroup(g)/(n-1)
    local integer i2 = 0
    local integer i3 = 0
    set n = n-1
    call GroupAddGroup(g,g2)
    loop
        exitwhen i2 > i
        call KillUnit( GetNthOfGroup( g2, n ) )
        call GroupRemoveUnit(g2, GetNthOfGroup(g2,n))
        if( removeUnits == true) then
            call GroupRemoveUnit( g, GetNthOfGroup(g2,n))
        endif
        loop
            exitwhen i3 == n
            call GroupRemoveUnit(g2,FirstOfGroup(g2))
            set i3 = i3 + 1
        endloop
        set i2 = i2 + 1     
    endloop    
endfunction
by the way I tried:
JASS:
function GetNthOfGroup takes group g,integer i returns unit
    local group g2
    local integer i2 = 0
    call GroupAddGroup(g2,g)
    loop
        call GroupRemoveUnit(g,FirstOfGroup(g))
        set i2 = i2 + 1
        if( i2 == i )then
            set i2 = 0
        endif
        exitwhen i2==0
    endloop
    return FirstOfGroup(g2)
endfunction

function KillEveryNthUnitInGroup takes group g, integer n, boolean removeUnits returns nothing
    local group g2
    local integer i = CountUnitsInGroup(g)/(n-1)
    local integer i2 = 0
    local integer i3 = 0
    set n = n-1
    call GroupAddGroup(g2,g)
    loop
        exitwhen i2 > i
        call KillUnit( GetNthOfGroup( g2, n ) )
        call GroupRemoveUnit(g2, GetNthOfGroup(g2,n))
        if( removeUnits == true) then
            call GroupRemoveUnit( g, GetNthOfGroup(g2,n))
        endif
        loop
            exitwhen i3 == n
            call GroupRemoveUnit(g2,FirstOfGroup(g2))
            set i3 = i3 + 1
        endloop
        set i2 = i2 + 1     
    endloop    
endfunction
aswell but it still didn't work
 
Level 19
Joined
Aug 24, 2007
Messages
2,888
you forgot CreateGroup()
local group g2 = CreateGroup()

hmm btw it should only kill the Nth unit
not 2Nth - 3Nth - 4Nth -5Nth etc
 
Level 5
Joined
Aug 16, 2007
Messages
149
ok ty for help but my script is supposed to kill 2nth 3nth etc. thats why it's KillEveryNthUnitInGroup not just KillNthUnitInGroup

EDIT
it still doesn't do anything more than once! what's the problem with my JASS????
 
Level 11
Joined
Feb 22, 2006
Messages
752
Sorry, but it seems to me like your script is way more complicated than it needs to be. Maybe I'm misunderstanding what you want the function to do, but here's my solution:

JASS:
function KillEveryNthUnitInGroup takes group g, integer n, boolean removeUnits returns nothing
    local group g2 = CreateGroup()
    local unit u
    local integer counter = 1
    call GroupAddGroup(g2,g)
    loop
        set u = FirstOfGroup( g2 )
        exitwhen ( u == null )
        if ( ModuloInteger( counter, n ) == 0 ) then
            call KillUnit( u )
            if( removeUnits ) then
                call GroupRemoveUnit( g, u )
            endif
        endif
        call GroupRemoveUnit( g2, u )
        set counter = counter + 1
    endloop
    call DestroyGroup( g2 )
    set g2 = null
endfunction

Basically, it kills every nth unit in the group specified. So, if n = 3, then it would kill the 3rd, 6th, 9th, etc. units.

What it does is it goes through every unit in the group and couples each unit with the integer counter. (actually, the integer just keeps track of which successive unit - 1st, 2nd, 3rd, etc. - in the group it is) If counter is divisible by n, then it kills that unit. And if removeunit is true then it removes the unit from the master group as well.
 
Last edited:
Level 5
Joined
Aug 16, 2007
Messages
149
?

Sorry, but it seems to me like your script is way more complicated than it needs to be. Maybe I'm misunderstanding what you want the function to do, but here's my solution:

JASS:
function KillEveryNthUnitInGroup takes group g, integer n, boolean removeUnits returns nothing
    local group g2 = CreateGroup()
    local unit u
    local integer counter = 1
    call GroupAddGroup(g2,g)
    loop
        set u = FirstOfGroup( g2 )
        exitwhen ( u == null )
        if ( ModuloInteger( counter, n ) == 0 ) then
            call KillUnit( u )
            if( removeUnits ) then
                call GroupRemoveUnit( g, u )
            endif
        endif
        call GroupRemoveUnit( g2, u )
        set counter = counter + 1
    endloop
    call DestroyGroup( g2 )
    set g2 = null
endfunction
Basically, it kills every nth unit in the group specified. So, if n = 3, then it would kill the 3rd, 6th, 9th, etc. units.

What it does is it goes through every unit in the group and couples each unit with the integer counter. (actually, the integer just keeps track of which successive unit - 1st, 2nd, 3rd, etc. - in the group it is) If counter is divisible by n, then it kills that unit. And if removeunit is true then it removes the unit from the master group as well.
yeah thats what i'm supposed to do but... this doesn't actually work...
 
Level 11
Joined
Feb 22, 2006
Messages
752
Wow, ok I am an idiot and can't read parameter descriptions right.

JASS:
function KillEveryNthUnitInGroup takes group g, integer n, boolean removeUnits returns nothing
    local group g2 = CreateGroup()
    local unit u
    local integer counter = 1
    set bj_wantDestroyGroup = false //if this value is set to true then your master group would be destroyed, so we make sure it is false
    call GroupAddGroup(g,g2) //function GroupAddGroup takes group sourceGroup, group destGroup returns nothing
    loop
        set u = FirstOfGroup( g2 )
        exitwhen ( u == null )
        if ( ModuloInteger( counter, n ) == 0 ) then
            call KillUnit( u )
            if( removeUnits ) then
                call GroupRemoveUnit( g, u )
            endif
        endif
        call GroupRemoveUnit( g2, u )
        set counter = counter + 1
    endloop
    call DestroyGroup( g2 )
    set g2 = null
endfunction

So basically the GroupAddGroup() function takes the SOURCE group first, not the destination group. It should work now.
 
Level 40
Joined
Dec 14, 2005
Messages
10,532
Setting bj_wantDestroyGroup to false is pointless... if you leave random useless set bj_wantDestroyGroup = true calls in your map, you should be recoding your map, not protecting yourself against them...

Here's my version of the code... (tweaked yours for efficiency and such)

JASS:
//To use this, you must do
//set <groupVar> = KillEveryNthUnitInGroup(<groupVar>,other params)
//This makes it a good deal faster, as it only has to go through the group once instead of twice

function KillEveryNthUnitInGroup takes group g, integer n, boolean removeUnits returns group
    local group g2 = CreateGroup()
    local unit u
    local integer counter = 1
    loop
        set u = FirstOfGroup(g)
        exitwhen u == null
        call GroupAddUnit(g2,u)
        if ModuloInteger(counter,n) == 0 then
            call KillUnit(u)
            if removeUnits then
                call GroupRemoveUnit(g2,u)
            endif
        endif
        call GroupRemoveUnit(g,u)
        set counter = counter + 1
    endloop
    call DestroyGroup(g)
    set g = g2
    set g2 = null
    return g
endfunction
 
Status
Not open for further replies.
Top