• 🏆 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] bj_lastCreatedGroup in FirstOfGroup loops

Status
Not open for further replies.
Level 6
Joined
Oct 23, 2011
Messages
182
a lot of people seem to be using bj_lastCreatedGroup.. to save a group handle?

JASS:
                    call GroupEnumUnitsInRange(bj_lastCreatedGroup,this.x,this.y,collsion_size,null)
                    loop
                        set u=FirstOfGroup(bj_lastCreatedGroup)
                        exitwhen u==null
                        call GroupRemoveUnit(bj_lastCreatedGroup,u)
                        if SomeCondition then

the problem is, sometimes I have another FirstOfGroup loop called from functions inside the action. This often collides and is annoying to debug.
any solutions? (this isnt just with my systems.. snippets in jass section used it too)
 
The answer seems obvious, use several groups.
It's not like if there is an other way.

This.

Snippets in the Jass section only use bj_lastCreatedGroup because we don't like creating a group just for an enumeration (We'll save on handles this way)

If you're doing FirstOfGroup loops within the FirstOfGroup loop, create another group.
OR, you can search blizzard.j to find some other global group : P
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
If you have a loop within a loop use two global groups. bj_lastCreatedGroup is possible for one of them though I'm not going to reject a resource if it doesn't use it (that would be insane).

However, it doesn't "f*** up" GUI scripts. No GUI user is going to get the value of (Last created group) except for immediately after a group of units are created, which is the exact use you'd use it for in JASS (immediately, not relying on timers or waits to remember the contents of that group).

GroupUtils went quite far with the ENUM_GROUP variable which is historical evidence that using a global group is great. Unfortunately the idea behind GroupUtils is not practical in many cases where you need the fastest and most logical looping possible which is why I made GroupMacro. Just use a second global group from within that loop if you need it. And by a global group I mean declaring it to a new group handle, and never destroying it, like this:

JASS:
globals
    private group GROUP_2 = CreateGroup()
endglobals
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
You will get a problem if the group function can be run recursively though. Or if you call some function that wants to use the group itself.

Do not think it would be senceful to abuse the blizzard.j variables, this already looks bad in semantics.

+ 42

And you can still push something hard in the ass of gui adepts, simply because you can't expect that a gui user use a variable instead of always "last group".
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
(Last created group) creates a new group each time it is used, so yeah the user will either set that to a variable or get their resource rejected.

JASS:
function GetLastCreatedGroup takes nothing returns group
    set bj_groupLastCreatedDest = CreateGroup()
    call ForGroup(bj_lastCreatedGroup, function GetLastCreatedGroupEnum)
    return bj_groupLastCreatedDest
endfunction
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
o_O, why the heck it always creates a new group ...
That doesn't make any sense, or i'm missing something ?

Anyway my point was not with an official resource, just about a random user.

// creates a new group in gui, we will name it "g"

// use a jass "system" but don't care about the group it used

// use again "g" but with the last created group function == fail.

EDIT : But that will be fine if you use bj_groupLastCreatedDest (i haven't checked other GUI functions though, i just mean that is ok with this function)
 

Bribe

Code Moderator
Level 50
Joined
Sep 26, 2009
Messages
9,464
Can you give a more practical example? If the user gets the value of (Last created group) after a wait then it doesn't matter if there's a JASS script using it or not, even a GUI script could mess it up at that point. All the GUI script would have to do is create a unit:

JASS:
function CreateNUnitsAtLoc takes integer count, integer unitId, player whichPlayer, location loc, real face returns group
    call GroupClear(bj_lastCreatedGroup)
    loop
        set count = count - 1
        exitwhen count < 0
        call CreateUnitAtLocSaveLast(whichPlayer, unitId, loc, face)
        call GroupAddUnit(bj_lastCreatedGroup, bj_lastCreatedUnit)
    endloop
    return bj_lastCreatedGroup
endfunction
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
I still believe that saving 32 bits of memory usage isn't worth the use of an ugly bj_
Plus, like i've said you can break a gui code, yes you can argue that a such broken script has already leaks, but leaks are not such an issue, most of times.

Hell, i still don't get why blizzard made a such Get last created group function, i thought it just returned bj_lastCreatedGroup.
GUI will never cease to amaze me.
 
Status
Not open for further replies.
Top