• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Group returns zero on second run

Status
Not open for further replies.
Level 4
Joined
Dec 10, 2005
Messages
73
First time it runs, everything is ok. But udg_TSp_Group[1] becomes corrupted. It returns all zero when ask to how many unit in udg_TSp_Group[1] . What's wrong?

JASS:
function Trig_Check_1 takes nothing  returns nothing

    local group g
    local unit u
    local location p
    
    // Remove all diagonal neighbours and place them in Group 2
             
        set g = udg_TSp_Group[1]
        loop
            set u = FirstOfGroup(g)
              
            exitwhen u == null       
            set p = GetUnitLoc(u)
            if ( DistanceBetweenPoints(udg_TSp_Point[1], p) >= 265.00 ) then
                call GroupAddUnit(udg_TSp_Group[2], u)
                call GroupRemoveUnit(udg_TSp_Group[1], u)                
            endif            
            call RemoveLocation( p )
            
            call GroupRemoveUnit(g,u)

        endloop    
    
    call DestroyGroup(g)
    set g = null
endfunction
 
Level 5
Joined
Dec 20, 2008
Messages
67
well you destroyed the global group...so it returns nulll
clear it instead...

JASS:
call DestroyGroup(g) //bad
call GroupClear(g) //good

you should only destroy local groups ... but in your case you destroy the global one..
 
Level 4
Joined
Dec 10, 2005
Messages
73
By the same logic, clearing g would clear the global one too. But at least, it's not destroyed.

I thought by doing this
JASS:
set g = udg_TSp_Group[1]
Would make a copy of the global group. Like integers or strings. Apparently not.
How would I make a copy of group without damaging the global one?
 
Level 14
Joined
Nov 23, 2008
Messages
187
It happens, because your're destroying that group.

JASS:
set g = udg_TSp_Group[1]
It doesn't copy the group, it just assigns pointer to that group.

There are pretty simple way of implementing routine you need:

JASS:
function TSp_Group_Check takes nothing returns nothing
  local unit     u = GetEnumUnit()
  local location p = GetUnitLoc(u)
  if DistanceBetweenPoints(udg_TSp_Point[1], p) >= 265.0 then
    call GroupAddUnit   (udg_TSp_Group[2], u)
    call GroupRemoveUnit(udg_TSp_Group[1], u)
  endif
  call RemoveLocation(p)
  set p = null
  set u = null
endfunction

function Trig_Check_1 takes nothing returns nothing
  call ForGroup(udg_TSp_Group[1], function TSp_Group_Check)
endfunction

Or, if you needed in group copy:

JASS:
// udg_temp_group is global
function AddToGroup takes nothing returns nothing
  call GroupAddUnit(udg_temp_group, GetEnumUnit())
endfunction

function Trig_Check_1 takes nothing returns nothing
  call GroupClear(udg_temp_group)
  call ForGroup(udg_TSp_Group[1], function AddToGroup)
  // your actions with udg_temp_group here
endfunction
 
Level 7
Joined
Jul 20, 2008
Messages
377
PenKnight: Handles are like pointers. What you're doing is what is called shallow copying, which is where you simply copy the memory address of another object to the pointer value of the handle.

In your case, you can imagine you're copying the memory address of that global group to g, which now points to the global group.

A deep copy, on the other hand, consists of copying the dereferenced data to a new instance of that object type.

As M0RT shows, you have to create a new group and then add all the units from the global group to that group. That's deep copying - you're copying the data of the global group instead of just the address of that group in memory.
 
Level 4
Joined
Dec 10, 2005
Messages
73
How interesting, So it's the same thing with all variable then. I'm going to play around with this with other variables types then and see what happens.

I happen to miss this in most of the tuts.
JASS:
 local group g = CreateGroup()

Thanks guys
 
Level 6
Joined
Sep 5, 2007
Messages
264
I found this out myself a while back. It took a bit to figure out just what was going on. Mort's method is the most efficient way that I know of to fix it.

@Soga: That's a very good way a explaining what's going on. I'll even +rep you for it.
 
Level 7
Joined
Jul 20, 2008
Messages
377
How interesting, So it's the same thing with all variable then. I'm going to play around with this with other variables types then and see what happens.

I happen to miss this in most of the tuts.
JASS:
 local group g = CreateGroup()

Thanks guys

Not all variables. Just handle variables. Handles are the pointers, but other things - like primitive data types (integer, boolean, real, string, etc.) are not like handles. You can do integer i = integer j and i will only take on the value of j, it won't "refer" to j from that point on.
 
Status
Not open for further replies.
Top