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.

      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