First of group vs. ForGroup II

Vexorian

Why no custom sig?
Reaction score
187
I dunno why that thread was closed. But I had to say this.

Let me say this, FirstOfGroup is faster than ForGroup when you don't need to keep the group after the iteration.

Else it is slower.

But the difference is negligible and FirstOfGroup has the advantage of not using another function so code looks more structured and you can easily use local variables without going through the work of passing them using bridge globals.

In fact, using bridge globals may as well make ForGroup heavier.

But if you ask me, I would rather not use groups at all, when you use ForGroup it means you want an iteration and groups are bad for that. And I have seen issues arise with FirstOfGroup if there are waits between the loop and one unit dies.

It is all easier to use a "dynamic array". Unit group is barely useful for IsUnitInGroup, and that's not so useful either. I am lately just using groups for quick detections in areas and ranges but never to actually store groups of units for later use.

BTW, the old benchmark ForGroup benchmark didn't use GetEnumUnit so it could as well be flawed.
 

Andrewgosu

The Silent Pandaren Helper
Reaction score
716
And if speed is not what you want, use the method you like more - the one which is more convenient.
 

PurgeandFire

zxcvmkgdfg
Reaction score
509
Thanks for the info Vex.

Well, I don't think this'll be a problem anymore. This should probably be stickied.
 

ReVolver

Mega Super Ultra Cool Member
Reaction score
609
I guess this will prevent the future arguments about groups.

[Thread Stuck]
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Stickied?
There are tons of helpful posts / threads in the Jass forum. If they were all stickied you would have to dive deep to find questions around here. Like all helpful threads, I remember them, if someone is asking a question refering to this, I'll point it out.

No need to sticky is one in my opinion. But, of course, I'm no moderator.

By the way Vexorain, would you mind explaining why exactly is that so? And how did you come to that conclusion?
 

ReVolver

Mega Super Ultra Cool Member
Reaction score
609
Well we should have at least add it to the "The JASS Forum" thread so we can won't have any problem or fights over which group method is better.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
There are lots and lots of arguements in Jass. Most of them were answered, yet new threads pop around asking the same question over and over becuase people don't bother searching. Does it mean we should sticky every answer to every arguement? In my opinion no, just point out the place with the answer, that is why we come to TH, to help. :p
 

ReVolver

Mega Super Ultra Cool Member
Reaction score
609
You got a point there but if only all the member can just read the sticky threads and the WE-Tutorial site.
 

Cohadar

master of fugue
Reaction score
209
It is all easier to use a "dynamic array".

I agree with this but it is not possible always.
Sure when you make say a spell that has some periodic timer manipulation of units, using arrays is almost always best solution.

But when you want to cast some spell on all units in an area for example you simply have to use groups.

After all, all GetUnitsInA... functions return groups.

I personally have never used loops for groups.

First because it is in my opinion ugly, I would always prefer two smaller functions over a single long one, it makes the code easier to understand.

Second because it destroys the group and I usually need it after that.

Sure passing global params can be a hussle, but I usually declare them just above ForGroup handler function so than I have the advantage of always knowing what are the parameters that I need when dealing with that group.

And since those parameters are usually locals in function that calls ForGroup
I then can remove those locals and use globals directly.
So there is no passing of arguments :)

JASS:

//===========================================================================
globals
    // global ForGroup parameters
    private group gp_group
    private unit gp_hero
    private integer gp_level
endglobals

//===========================================================================
private function GroupStun takes nothing returns nothing
    local real x2 = GetUnitX(gp_hero)
    local real y2 = GetUnitY(gp_hero)
    //---- 
    set lift = DeltaImpulse.create(GetEnumUnit(), 0.0, 0.0, 300.0 + AOE(gp_level) - distance)
    //---- 
endfunction

//===========================================================================
//  In spell Actions I am using gp_ globals directly, no need for locals
//===========================================================================
private function Actions takes nothing returns nothing
    set gp_hero = GetTriggerUnit()
    set gp_group = NewGroup() // Recycler
    set gp_level = GetUnitAbilityLevel(gp_hero, AID_WAR_STOMP)
    //----   
    call GroupEnumUnitsInRange(gp_group, x, y, AOE(cdata.level), Condition(function GroundEnemyCondition))
    call ForGroup(gp_group, function GroupStun)
    //----
endfunction


//---- lines represent code I removed for simplicity

So if you ask me: ForGroup > First of group
 

juty

Zoom out problems look smaller
Reaction score
26
There are lots and lots of arguements in Jass. Most of them were answered, yet new threads pop around asking the same question over and over becuase people don't bother searching. Does it mean we should sticky every answer to every arguement? In my opinion no, just point out the place with the answer, that is why we come to TH, to help. :p

Easy to fix that don’t let users join the website until they activate there account by email and say in email to search form first because things are quicker this way saves time I recon
 

NetherHawk

New Member
Reaction score
26
hmmm, i use a mixture of both, it depends on your spell actually yes? and what you are trying to achieve. In certain cases, forGroup really is faster. in instances where you only need to use the group once.
 

Vexorian

Why no custom sig?
Reaction score
187
But when you want to cast some spell on all units in an area for example you simply have to use groups.

You can just use the boolexpr argument of any of the GroupEnum functions to do the iteration or even add them to the unit array.

After all, all GetUnitsInA... functions return groups.
I wouldn't really use any of those GetUnitsIn BJ functions, they most likely keep creating a group, or don't null something/etc. And since GroupEnum functions are pretty messed up when you use a group that is not permanent it is not good to use them.



I personally have never used loops for groups.
First because it is in my opinion ugly, I would always prefer two smaller functions over a single long one, it makes the code easier to understand.
Second because it destroys the group and I usually need it after that.

And that just about does not make much sense, if GroupEnums are the only good reason to use groups, you don't really need to keep the group. You will have to detect again, otherwise, use your iterating method to store units in an array, linked list, whatever that isn't a group.

--
My conclusion is that both ForGroup and FirstOfGroup mean that you are actually using groups for something that is not detecting units in rect or range, and therefore could be replaced by better things.

When I need to enumerate units in range or rect I just use a boolexpr, ever since we found out that Condition always makes the best handle it just got easier to use just Enum than Enum + ForGroup . Less lines, and still two functions, quite fun. And when I use Enums I use the same ENUMGROUP group global variable so I don't have issues with them. By having a return false in such group enums I prevent any unit from being added and thus there are less linking and no usage of memory that would otherwise be caused by the usual way that requires both an Enum and a ForGroup .

Out of over-complication I made a function that is like GetUnitsInRangeOfLoc but that takes a dynamic unit array argument to store the units there. It is probably slower than anything else but I minimize the problems with groups.

And I was missing the point here, there are issues with unit groups, and there specially are issues with FirstOfGroup (issues, as if random bugs when a disease cloud is nearby ...) And this is the main reason I just avoid them for storage of units and just use them for detection.
 

Somatic

You can change this now in User CP.
Reaction score
84
Nice Points put out. But if using a dynamic Array for units, How do you do that? Mind showing an example?
 

Doom-Angel

Jass User (Just started using NewGen)
Reaction score
167
what's a private global and private function?
i never heard of those
 

Cohadar

master of fugue
Reaction score
209
You can just use the boolexpr argument of any of the GroupEnum functions to do the iteration or even add them to the unit array.

Now THIS is useful information.
Really nice idea, working on units directly while they are being picked...

I use GroupEnumUnitsInRect and GroupEnumUnitsInRegion + group recycler,
no BJ's there.

Using array is like you said slower in here but with direct boolexpr using it might not be necessary...

Passing of global params with boolexpr is still the same as with ForGroup I believe?

@Doom-Angel:
jassnewgenpack4b\jasshelper\jasshelpermanual.html

EDIT:
Vell Vex I tried this boolexpr thing and I can say I do not like it now.
It is merging together GroupCondition function and a EnumGroupAction functions.
I would always prefer better looking over slightly optimized code,
it simply helps you not make mistakes.
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
> What are the odds of such things happening

About one in a million, which makes them happen nine games out of ten, and the one in ten are those you are playing.
 
Reaction score
456
[Off-Topic]

Some was talking about stickying this thread. I was just thinking that wouldn't it be better to make a "Frequently Asked ("JASS") Questions" thread, where these ("future")helpful posts are collected?
 

N-a-z-g-u-l

New Member
Reaction score
30
There are lots and lots of arguements in Jass. Most of them were answered, yet new threads pop around asking the same question over and over becuase people don't bother searching.

well, that isnt right... somehow the last threads about this topic were closed too early, just before the discussion started...

its funny that a thread created by vexorian will instantly be stickied, even if there are no proofs in it at all... it just a thesis and even should be an answer to another thread...
but well, you seem to trust in vexorian... then sticky it and ignore proven posts...

by the way, is it even possible to call GroupEnumUnitsInRect(null, r, Condition( function ... ) )?

JASS:
function ForUnitsInRect takes rect R, code C returns nothing
    local group G = CreateGroup()
    call GroupEnumUnitsInRect(G, R, Condition( C ) )
    call DestroyGroup( G )
    set G = null
endfunction


and how will it affect performance if i just use no filter ( null? )

EDIT: updated script with a group instead of "null" as group parameter
 

Cohadar

master of fugue
Reaction score
209
Don't use null for a group.
Just use some dummy global group witch you will instantly clear after you enum all units
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top