ForGroup - code callback problems.

T

Tooters

Guest
I want to recreate the Starcraft Trigger "Bring" in Jass, and I have some questions about the ForGroups. I don't want to make another function because then I would have to use handles to use the same groups and things like that so I tried putting ( function GroupAddUnitSimple( GetEnumUnit( ), bringGroup ) ) for the part where it says CODE CALLBACK. I did this in the IF COUNTER >= QUANTITY THEN part also. I also tried using a native.. but I don't think the natives would work. At all the spots it gave Syntax Error, Can someone please help me?

Code:
function bring takes player whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup
    local integer counter
    call ForGroup( GetUnitsInRectOfPlayer( whichRect, whichPlayer ), function GroupAddUnitSimple( GetEnumUnit( ), bringGroup ) )  
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, function GroupRemoveUnitSimple( GetEnumUnit( ), bringGroup ) )
        call ForGroup( bringGroup, native RemoveUnit( GetEnumUnit( ) ) )
        return true
    endif
    return false
endfunction
 

Chocobo

White-Flower
Reaction score
409
Code:
function bring takes player whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup
    local integer counter
    call ForGroup( GetUnitsInRectOfPlayer( whichRect, whichPlayer ), [B]function[/B] GroupAddUnitSimple( GetEnumUnit( ), bringGroup ) )  
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, [B]function[/B] GroupRemoveUnitSimple( GetEnumUnit( ), bringGroup ) )
        call ForGroup( bringGroup, [B]native[/B] RemoveUnit( GetEnumUnit( ) ) )
        return true
    endif
    return false
endfunction

There are no name like in a function.

Code:
function bring takes player whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup
    local integer counter
    call ForGroup( GetUnitsInRectOfPlayer( whichRect, whichPlayer ), GroupAddUnitSimple( GetEnumUnit( ), bringGroup ) )  
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, GroupRemoveUnitSimple( GetEnumUnit( ), bringGroup ) )
        call ForGroup( bringGroup, RemoveUnit( GetEnumUnit( ) ) )
        return true
    endif
    return false
endfunction

I think it works.
 

Jazradel

Helping people do more by doing less.
Reaction score
102
ForGroup only works with functions that don't take anything. So:

function Remove takes nothing returns nothing
call RemoveUnit( GetEnumUnit( ) )
endfunction

call ForGroup( bringGroup, function Remove )

Would be how to do it.

I recommend creating some GUI triggers and converting them over or using the loop method of emualting a group found here:http://www.wc3jass.com/viewtopic.php?t=1998
 

Chocobo

White-Flower
Reaction score
409
Hmm, didn't see ForGroup.. this should work perfect

Code:
function bring takes player whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup
    local integer counter
    call ForGroup(GetUnitsInRectOfPlayer(whichRect, whichPlayer ), function GroupAddUnitSimple )  
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup(bringGroup, function GroupRemoveUnitSimple )
        call ForGroup(bringGroup, function RemoveUnit )
        return true
    endif
    return false
endfunction
 
T

Tooters

Guest
-Chocobo, how will it know what groups to add and remove from, and which unit to do that to doing it your way?

-Jazradel, that would work fine for removing the unit, but what about adding/removing the unit from the unit group? I would have to make the variable global, or use handles and I don't want to do that.
 

corvusHaunt

New Member
Reaction score
96
Tooters said:
-Jazradel, that would work fine for removing the unit, but what about adding/removing the unit from the unit group? I would have to make the variable global, or use handles and I don't want to do that.
Are you talking about the handle vars? For units you don't have to do that, you can just store them in the game cache with Blizzard's function. Don't really need either though, I'm wondering why this is a problem.

Okay, the handle 'code' doesn't allow you to add the parameters, so you need to do something like this.

Code:
function BringGroupRemove takes nothing returns nothing
call GroupRemoveUnitSimple( GetEnumUnit(), GetLastCreatedGroup() )
endfunction

function bring takes player whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup
    local integer counter
    call ForGroup( GetUnitsInRectOfPlayer( whichRect, whichPlayer ), function GroupAddUnitSimple( GetEnumUnit( ), bringGroup ) )  
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, function BringGroupRemove)
        return true
    endif
    return false
endfunction

*You can replace EnumUnit if you want by storing the unit in the Game cache
*Is this function supposed to remove every unit if the counter is greater than the quantity?
 
T

Tooters

Guest
Code:
function add2Group takes nothing returns nothing
    call GroupAddUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
endfunction

function remove takes nothing returns nothing
    call GroupRemoveUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
    call RemoveUnit( GetEnumUnit( ) )
endfunction

function bring takes player whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup = CreateGroup( )
    local integer counter
    call ForGroup( GetUnitsInRectOfPlayer( whichRect, whichPlayer ), function add2Group )  
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, function remove )
        return true
    endif
    return false
endfunction

that is what I have now. There is only 1 problem with this code. I also want to check if it is the right TypeId, there is no GetUnitsInRectOfPlayerOfTypeId, or anything like that. So this is the part where I get stumped because I don't want to use handles/global variables.
 

corvusHaunt

New Member
Reaction score
96
That's because the GUI actions that say "Get every unit matching blah blah, Get every unit in loc blah" all use ForGroup, then the code callback is the condition.

For what you want, you will need something like this:

Code:
[COLOR=Blue]function CheckOwnerAndID takes nothing returns boolean
    return GetOwningPlayer(GetEnumUnit())==Player(bj_forceCountPlayers) and GetUnitTypeId(GetEnumUnit())=='id'
endfunction[/COLOR]

function add2Group takes nothing returns nothing
    call GroupAddUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
endfunction

function remove takes nothing returns nothing
    call GroupRemoveUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
    call RemoveUnit( GetEnumUnit( ) )
endfunction

function bring takes player whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup = CreateGroup( )
    local integer counter
     [COLOR=Purple]set bj_forceCountPlayers=(theplayernumber)[/COLOR]
[COLOR=Blue]    call ForGroup( GetUnitsInRectMatching(whichRect, whichPlayer),Condition(CheckOwnerAndID),function add2Group) [/COLOR] 
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, function remove )
        return true
    endif
    return false
endfunction
Should work :)
 
T

Tooters

Guest
Code:
function CheckOwnerAndID takes integer unitId returns boolean
    return GetOwningPlayer( GetEnumUnit( ) ) == Player( bj_PLAYER_NEUTRAL_EXTRA ) and GetUnitTypeId( GetEnumUnit( ) ) == unitId
endfunction

function add2Group takes nothing returns nothing
    call GroupAddUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
endfunction

function remove takes nothing returns nothing
    call GroupRemoveUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
    call RemoveUnit( GetEnumUnit( ) )
endfunction

function bring takes integer whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup = CreateGroup( )
    local integer counter
    set bj_PLAYER_NEUTRAL_EXTRA = whichPlayer
    [B]call ForGroup( GetUnitsInRectMatching( whichRect, Condition( CheckOwnerAndID( unitId ) ) ), function add2Group)  [/B]     
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, function remove )
        return true
    endif
    return false
endfunction

That is now what I have, but there is an error at line 18 ( bolded ) that says:

Line 18: Cannot assign to constant bj_PLAYER_NEUTRAL_EXTRA
Line 18: Cannot convert boolean to code

so what could i replace that with?
 

corvusHaunt

New Member
Reaction score
96
Yep, I noticed that after and changed it. I was trying to get a bj_ value in there to avoid using globals, and forgot that it was a constant, use bj_forceCountPlayers instead, then you can set it to zero after.
Code to ID thing, 'Condition' still takes code, so you still can't use parameters, use instead:

Code:
function CheckOwnerAndID takes integer unitId returns boolean
    return GetOwningPlayer( GetEnumUnit( ) ) == Player( bj_PLAYER_NEUTRAL_EXTRA ) and GetUnitTypeId( GetEnumUnit( ) ) == bj_randomSubGroupWant
endfunction

function bring takes integer whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup = CreateGroup( )
    local integer counter
    set bj_PLAYER_NEUTRAL_EXTRA = whichPlayer
    set bj_randomSubGroupWant=unitId
    call ForGroup( GetUnitsInRectMatching( whichRect, Condition( CheckOwnerAndID( unitId ) ) ), function add2Group)  
    set bj_PLAYER_NEUTRAL_EXTRA =0
    set bj_randomSubGroupWant=0
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, function remove )
        return true
    endif
    return false
endfunction
 
T

Tooters

Guest
Code:
function CheckOwnerAndID takes nothing returns boolean
    return GetOwningPlayer( GetEnumUnit( ) ) == Player( bj_forceCountPlayers ) and GetUnitTypeId( GetEnumUnit( ) ) == bj_randomSubGroupWant
endfunction

function add2Group takes nothing returns nothing
    call GroupAddUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
endfunction

function remove takes nothing returns nothing
    call GroupRemoveUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
    call RemoveUnit( GetEnumUnit( ) )
endfunction

function bring takes integer whichPlayer, integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup = CreateGroup( )
    local integer counter
    set bj_forceCountPlayers = whichPlayer
    set bj_randomSubGroupWant = unitId
    [B]call ForGroup( GetUnitsInRectMatching( whichRect, Condition( CheckOwnerAndID ) ), function add2Group ) [/B]    
    set bj_forceCountPlayers = 0
    set bj_randomSubGroupWant = 0
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, function remove )
        return true
    endif
    return false
endfunction

Line 19: Undeclared Variable: CheckOwnerAndID
 

corvusHaunt

New Member
Reaction score
96
Dang I am getting sloppy :/

Use:

Code:
 call ForGroup(GetUnitsInRectMatching(whichRect,Condition( CheckOwnerAndID())),function add2Group)
 
T

Tooters

Guest
Thx for your help, but now I need help putting it into a trigger. This is what I have.

Code:
Bring
    Events
        Unit - A unit enters Test <gen>
    Conditions
    Actions
        Custom script: if bring( 2, 'hpea', gg_rct_Test ) == true then
        Unit - Create 1 Footman for (Owner of (Entering unit)) at (Center of Test <gen>) facing Default building facing (270.0) degrees
        Custom script: endif

Nothing happens when I bring the peasants into the area.

Code:
function CheckOwnerAndID takes nothing returns boolean
    return GetOwningPlayer( GetEnumUnit( ) ) == Player( bj_forceCountPlayers ) and GetUnitTypeId( GetEnumUnit( ) ) == bj_randomSubGroupWant
endfunction

function add2Group takes nothing returns nothing
    call GroupAddUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
endfunction

function remove takes nothing returns nothing
    call GroupRemoveUnit( GetLastCreatedGroup( ), GetEnumUnit( ) )
    call RemoveUnit( GetEnumUnit( ) )
endfunction

function bring takes integer quantity, integer unitId, rect whichRect returns boolean
    local group bringGroup = CreateGroup( )
    local integer counter
    local integer whichPlayer = GetPlayerId( GetOwningPlayer( GetEnteringUnit( ) ) )
    set bj_forceCountPlayers = whichPlayer
    set bj_randomSubGroupWant = unitId
    call ForGroup( GetUnitsInRectMatching( whichRect, Condition( function CheckOwnerAndID ) ), function add2Group )   
    set bj_forceCountPlayers = 0
    set bj_randomSubGroupWant = 0
    set counter = CountUnitsInGroup( bringGroup )
    if counter >= quantity then
        call ForGroup( bringGroup, function remove )
        return true
    endif
    return false
endfunction
 
T

Tooters

Guest
yeah, there's 3 outside and then I move them into it 1 by 1 and it doesn't work.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • WildTurkey WildTurkey:
    is there a stephen green in the house?
    +1
  • The Helper The Helper:
    What is up WildTurkey?
  • The Helper The Helper:
    Looks like Google fixed whatever mistake that made the recipes on the site go crazy and we are no longer trending towards a recipe site lol - I don't care though because it motivated me to spend alot of time on the site improving it and at least now the content people are looking at is not stupid and embarrassing like it was when I first got back into this like 5 years ago.
  • The Helper The Helper:
    Plus - I have a pretty bad ass recipe collection now! That section of the site is 10 thousand times better than it was before
  • The Helper The Helper:
    We now have a web designer at my job. A legit talented professional! I am going to get him to redesign the site theme. It is time.
  • Varine Varine:
    I got one more day of community service and then I'm free from this nonsense! I polished a cop car today for a funeral or something I guess
  • Varine Varine:
    They also were digging threw old shit at the sheriff's office and I tried to get them to give me the old electronic stuff, but they said no. They can't give it to people because they might use it to impersonate a cop or break into their network or some shit? idk but it was a shame to see them take a whole bunch of radios and shit to get shredded and landfilled
  • The Helper The Helper:
    whatever at least you are free
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top