• 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] Jass function help

Status
Not open for further replies.
I know i've posted this before, but it's not solved yet, and i really want it to work.
I'm making a little grouping system for my map, and i have a function that checks for a unit of specific type inside a group, and, if successful, returns it.
It works perfectly the first time, but it will only run once per group.

Here's the script:

JASS:
function getFreeUnit takes unit u returns unit
    local unit temp = null
    local group g = CreateGroup()
    if GetUnitUserData(u)!=0 then
    set g = udg_sq_g[GetUnitUserData(u)]
    loop
    exitwhen GetUnitTypeId(temp) == 'h002'
    set temp = FirstOfGroup(g)
    if GetUnitTypeId(temp) != 'h002' then
    call GroupRemoveUnit(g, temp) 
    else
    return temp
endif 
    endloop
    
    endif
    call DestroyGroup(g)
    set g = null
    return temp
endfunction

I know it leaks a unit handle, but i really don't know how to fix that sin'ce it's the return.

But what's wrong whith this script?
I'd be thankful for answers.

EDIT: I forgot to say, the gruop is an array indexed by it's members custom value. So if input unit is in group 3, the function will look for units of that type in group 3 in the array.
 
Last edited:
Level 8
Joined
Feb 15, 2009
Messages
463
JASS:
function getFreeUnit takes unit u returns unit
    local unit d
    local group g = CreateGroup()

    if GetUnitUserData(u)!=0 then

        set g = udg_sq_g[GetUnitUserData(u)]
// why a global with udg ? get JNGP

    loop
        set d = FirstOfGroup(g)
        exitwhen  d == null

             if GetUnitTypeId(d) != 'h002' then

                 call GroupRemoveUnit(g, d) 

                 else
                 call DestroyGroup(g)
                 set g = null
                 return d

             endif 
    endloop
    
    endif
    call DestroyGroup(g)
    set g = null
    return d
endfunction

Also im not sure about that try my sheet
 
Last edited:
Level 11
Joined
Feb 22, 2006
Messages
752
Both of your codes leak a group.

JASS:
function getFreeUnit takes unit u returns unit
    local unit temp = null
    local group g = CreateGroup() // you create a group here
    if GetUnitUserData(u)!=0 then
        set g = udg_sq_g[GetUnitUserData(u)] //If this runs, variable g now points to the group that the global points to...the new group that g was pointing to is now out of scope
    // ...
    call DestroyGroup(g) //if the code inside the if block runs, the group that the global points to is destroyed, but the group that was created in this function is left undestroyed
endfunction
 
The reason it works only once is because you destroy the group with your function.

Isn't the local variable "g" just an instance of "udg_sq_g"?
I mean, i thought i set it to the same value, kind of like copying it.
And how should the code look for it to work?

Thanks for the group leak notifycation btw whoever noted that.
 
Level 11
Joined
Feb 22, 2006
Messages
752
If you do

JASS:
set g = udg_sq_g

the variable g will point to the same group that udg_sq_g points to. The group isn't copied. To make a copy of a group, you need to create a new group and add to the new group all the units in the old one. I think there's a function GroupAddGroup() that allows you to do that.
 
Status
Not open for further replies.
Top