1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still haven't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Ride into the sunset with the 32nd Modeling Contest.
    Dismiss Notice
  4. This adventure has come to an end. Congratulate our heroes in the 16th Mini Mapping Contest Results.
    Dismiss Notice
  5. From the gates of hell, the 5th Special Effect Contest Results have emerged.
    Dismiss Notice
  6. Race against the odds and Reforge, Don't Refund. The 14th Techtree Contest has begun!
    Dismiss Notice
  7. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] Group returns zero on second run

Discussion in 'Triggers & Scripts' started by PenKnight, Feb 18, 2009.

  1. PenKnight

    PenKnight

    Joined:
    Dec 10, 2005
    Messages:
    73
    Resources:
    0
    Resources:
    0
    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?

    Code (vJASS):

    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
     
     
  2. Akolyt0r

    Akolyt0r

    Joined:
    Dec 20, 2008
    Messages:
    53
    Resources:
    3
    Icons:
    3
    Resources:
    3
    well you destroyed the global group...so it returns nulll
    clear it instead...

    Code (vJASS):

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


    you should only destroy local groups ... but in your case you destroy the global one..
     
  3. PenKnight

    PenKnight

    Joined:
    Dec 10, 2005
    Messages:
    73
    Resources:
    0
    Resources:
    0
    By the same logic, clearing g would clear the global one too. But at least, it's not destroyed.

    I thought by doing this
    Code (vJASS):
    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?
     
  4. Shadow Daemon

    Shadow Daemon

    Joined:
    Nov 23, 2008
    Messages:
    184
    Resources:
    3
    Tools:
    3
    Resources:
    3
    It happens, because your're destroying that group.

    Code (vJASS):
    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:

    Code (vJASS):
    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:

    Code (vJASS):
    // 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
     
  5. M0RT

    M0RT

    Joined:
    Apr 15, 2008
    Messages:
    1,067
    Resources:
    6
    Maps:
    3
    StarCraft II Resources:
    1
    Tutorials:
    2
    Resources:
    6
    Copying the group:
    Code (vJASS):

     local group g = CreateGroup()
     GroupAddGroup( udg_TSp_Group[1], g )
     
     
  6. Soga-

    Soga-

    Joined:
    Jul 20, 2008
    Messages:
    374
    Resources:
    0
    Resources:
    0
    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.
     
  7. PenKnight

    PenKnight

    Joined:
    Dec 10, 2005
    Messages:
    73
    Resources:
    0
    Resources:
    0
    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.
    Code (vJASS):
     local group g = CreateGroup()


    Thanks guys
     
  8. Lord_BoNes

    Lord_BoNes

    Joined:
    Sep 5, 2007
    Messages:
    264
    Resources:
    0
    Resources:
    0
    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.
     
  9. Akolyt0r

    Akolyt0r

    Joined:
    Dec 20, 2008
    Messages:
    53
    Resources:
    3
    Icons:
    3
    Resources:
    3
    all handle variables
     
  10. Soga-

    Soga-

    Joined:
    Jul 20, 2008
    Messages:
    374
    Resources:
    0
    Resources:
    0
    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.