Looping Help

T

Tooters

Guest
I want some creeps to go attack the Castle after they enter a certain region. I have a loop that detects how many creeps are in the region, and when if that number should reach 0 then the gates close leading to the castle, but when they enter the area they attack the castle, and after I kill them all, the gates don't close. Here is my triggers:
Code:
Castle Area Copy
    Events
        Unit - A unit enters Human Encampment <gen>
    Conditions
        (Owner of (Entering unit)) Equal to Player 12 (Brown)
    Actions
        Destructible - Open Gate (Horizontal) 0000 <gen>
        Destructible - Open Gate (Horizontal) 0001 <gen>
        Destructible - Open Gate (Horizontal) 0002 <gen>
        Destructible - Open Gate (Horizontal) 0003 <gen>
        Destructible - Open Gate (Horizontal) 0004 <gen>
        Trigger - Turn off Hero Death <gen>
        Trigger - Turn on Hero Death2 <gen>
        Custom script: call Loop_Function()
        Set attackCastle = False

Here is Loop_Function

Code:
function Exit_When takes nothing returns boolean
    return ( udg_attackCastle == true )
endfunction

function Units_In_attackGroup takes nothing returns boolean
    if ( not ( CountUnitsInGroup(udg_attackGroup[0]) != 0 ) ) then
        return false
    endif
    return true
endfunction

function If_Statement takes nothing returns nothing
    if ( Units_In_attackGroup() ) then
    else
        set udg_attackCastle = true
        call DisableTrigger( gg_trg_Hero_Death2 )
        call EnableTrigger( gg_trg_Hero_Death )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0000 )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0001 )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0002 )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0003 )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0004 )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0000, true )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0001, true )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0002, true )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0003, true )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0004, true )
        call PauseTimerBJ( false, udg_creepTimer )
        call DestroyGroup(udg_attackGroup[0])
    endif
endfunction

function Loop_Function takes nothing returns nothing
    loop
    call PolledWait( 1.00 )
    set udg_attackGroup[0] = GetUnitsInRectOfPlayer(gg_rct_Human_Encampment, Player(11))
    exitwhen(Exit_When())
    call If_Statement()
    endloop
endfunction

Please help me figure this out.
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
No doubt about it, here is your problem ^^

Code:
call ForGroupBJ( udg_attackGroup[0], function If_Statement )
Runs the function If_Statement once for each unit in the group.

Code:
    if ( Units_In_attackGroup() ) then
    else
        set udg_attackCastle = true
        call DisableTrigger( gg_trg_Hero_Death2 )
        call EnableTrigger( gg_trg_Hero_Death )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0000 )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0001 )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0002 )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0003 )
        call ModifyGateBJ( bj_GATEOPERATION_CLOSE, gg_dest_LTg1_0004 )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0000, true )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0001, true )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0002, true )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0003, true )
        call SetDestructableInvulnerableBJ( gg_dest_LTg1_0004, true )
        call PauseTimerBJ( false, udg_creepTimer )
        call DestroyGroup(udg_attackGroup[0])
    endif

The else statement will only run if the group is empty, but if the group is empty then the function wont fun at all.

Just change
Code:
call ForGroupBJ( udg_attackGroup[0], function If_Statement )
to
Code:
call If_Statement()


I suggest that you read some tutorials on how to write more effective jass code, you can improve that code a lot.
 
T

Tooters

Guest
I did what you said but it still didn't work. :(

BTW: What's the difference between PolledWait, and TriggerSleepAction?
 

phyrex1an

Staff Member and irregular helper
Reaction score
447
What is the initial vaule of udg_attackCastle? If it is true from the begining then you have to move the line
Code:
Set attackCastle = False
one step up.

PolledWait uses a timer to keep track on the time remaining on the wait, thus it will pause the wait during lagg periods. But insteed it will last somewhat longer than expected.
I actualy suggest that you try to keep your functions as free from waits as possible, if you have to wait then use a timer.
 
T

Tooters

Guest
Well I changed PolledWait to TriggerSleepAction and it worked... o_O I already knew that it used a timer for PolledWait I just don't understand why it works with TriggerSleepAction and not PolledWait.

I use Waits so that the loop won't go off ever .01 seconds and lag the game.
 
T

Tooters

Guest
Well actually it still doesn't work. I just tested my map out again, and some units got spawned inside of the rect and I killed them and the gates did not close.
 
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