• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Solved] Array of Timers not Working

Status
Not open for further replies.
Level 4
Joined
Mar 20, 2014
Messages
67
Hey, I start an attack timer for 180 seconds, and at the end it's supposed to fire a trigger but it does nothing. Literally it's like the trigger isn't firing. Any help?
Here's the related two triggers.
JASS:
function TrigAttackSubstr takes nothing returns nothing
local string subs = SubString(GetEventPlayerChatString(), 8, StringLength(GetEventPlayerChatString()))
local string array color
local integer i = 0
local integer stop = GetPlayers()
local integer p = GetPlayerId(GetTriggerPlayer())
local rect array areas
local texttag t
local timerdialog tim
local unit u
set areas[0] = gg_rct_Player_1_Area
set areas[1] = gg_rct_Player_2_Area
set areas[2] = gg_rct_Player_3_Area
set areas[3] = gg_rct_Player_4_Area
set areas[4] = gg_rct_Player_5_Area
set areas[5] = gg_rct_Player_6_Area
set color[0] = "red"
set color[1] = "blue"
set color[2] = "teal"
set color[3] = "purple"
set color[4] = "yellow"
set color[5] = "orange"
if (stop == 1 ) then
        call DisplayTextToForce(bj_FORCE_PLAYER, "You don't have any enemies silly. Try the singleplayer attackers.")
else
    loop
        exitwhen i > stop
        if (StringCase(subs, false) == StringCase(color[i], false)) and (IsAttacking == false) and (p != i) and (GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING) then
            set IsAttacking = true
            call SetCameraBoundsToRect(areas[i])
            call SetCameraPosition(GetRectCenterX(areas[i]), GetRectCenterY(areas[i]))
            set u = CreateUnit(GetTriggerPlayer(), 'h00E', GetRectCenterX(areas[i]), GetRectCenterY(areas[i]), GetRandomDirectionDeg())
            call GroupAddUnit(AttackUnits, u)
            call SelectUnitForPlayerSingle(u, Player(p))
            set AttackTimer = CreateTimer()
            set timdia = CreateTimerDialog(AttackTimer)
            set timdia = CreateTimerDialogBJ( AttackTimer, "Time Remaining to Attack: " )
            call TimerDialogDisplayForPlayerBJ( true, timdia, GetTriggerPlayer() )
            set GoldAvail[i] = R2I((GetPlayerState(Player(i), PLAYER_STATE_RESOURCE_GOLD) * .2))
            set LumbAvail[i] = R2I((GetPlayerState(Player(i), PLAYER_STATE_RESOURCE_LUMBER) * .2))
            call TimerStart(AttackTimer, 20, false, null)
            call SavePlayerHandle(MUAC, 22, GetHandleId(AttackTimer), GetTriggerPlayer())
            call SavePlayerHandle(MUAC, 23, GetHandleId(AttackTimer), Player(i))
            call BJDebugMsg(I2S(p))
            call BJDebugMsg(I2S(i))
            set PlayerCurBuildDestroyed[i] = 0
            set i = stop + 1
        endif
        set i = i + 1
    endloop
endif
endfunction

//===========================================================================
function InitTrig_Attack takes nothing returns nothing
local integer i = 0
local integer stop = GetPlayers()
    set gg_trg_Attack = CreateTrigger(  )
    loop
        call TriggerRegisterPlayerChatEvent( gg_trg_Attack, Player(i), "-attack ", false )
        set i = i + 1
        exitwhen i == stop
    endloop
    call TriggerAddAction( gg_trg_Attack, function TrigAttackSubstr)
endfunction
JASS:
function killall takes nothing returns nothing
local unit u = GetEnumUnit()
    call KillUnit(u)
    call GroupRemoveUnit(AttackUnits[GetPlayerId(GetOwningPlayer(u))], u)
    call GroupRemoveUnit(ArmyUnits[GetPlayerId(GetOwningPlayer(u))], u)
    set u = null
endfunction

function ResetAnimation takes nothing returns nothing
local unit u
if (IsUnitType(GetEnumUnit(), UNIT_TYPE_STRUCTURE)) then
    set u = GetEnumUnit()
    call SetUnitAnimation(u, "stand work")
    call UnitRemoveAbility(u, 'Abun')
    call SetUnitPathing( GetEnumUnit(), true)
    call SetUnitInvulnerable(u, false)
    call SetWidgetLife(u, GetUnitState(u, UNIT_STATE_MAX_LIFE))
    call GroupRemoveUnit(AttackedUnits[GetPlayerId(GetOwningPlayer(u))], u)
    set u = null
endif
endfunction

function TimerExpire takes nothing returns nothing
local player d = LoadPlayerHandle(MUAC, 22, GetHandleId(GetExpiredTimer()))
local integer z = GetPlayerId(d)
local integer p = GetPlayerId(LoadPlayerHandle(MUAC, 23, GetHandleId(GetExpiredTimer())))
local integer bhealer = 'h00X'
local rect array areas
    set areas[0] = gg_rct_Player_1_Area
    set areas[1] = gg_rct_Player_2_Area
    set areas[2] = gg_rct_Player_3_Area
    set areas[3] = gg_rct_Player_4_Area
    set areas[4] = gg_rct_Player_5_Area
    set areas[5] = gg_rct_Player_6_Area
    call DisplayTextToForce(bj_FORCE_PLAYER[z], "Congratulations you've pillaged lumber and gold from your enemy!")
    call SetCameraBoundsToRectForPlayerBJ(Player(z), areas[z])
    call SetCameraPositionForPlayer(Player(z), GetRectCenterX(areas[z]), GetRectCenterY(areas[z]))
    call ForGroup(AttackedUnits, function ResetAnimation)
    call DestroyTimerDialog(timdia[z])
    set IsAttacking[z] = false
    call DestroyTimer(AttackTimer[z])
    call ForGroup(AttackUnits[z], function killall)
endfunction

//===========================================================================
function InitTrig_Timer_Expires takes nothing returns nothing
set gg_trg_Timer_Expires = CreateTrigger(  )
call TriggerRegisterTimerExpireEventBJ( gg_trg_Timer_Expires, AttackTimer[0] )
call TriggerRegisterTimerExpireEventBJ( gg_trg_Timer_Expires, AttackTimer[1] )
call TriggerRegisterTimerExpireEventBJ( gg_trg_Timer_Expires, AttackTimer[2] )
call TriggerRegisterTimerExpireEventBJ( gg_trg_Timer_Expires, AttackTimer[3] )
call TriggerRegisterTimerExpireEventBJ( gg_trg_Timer_Expires, AttackTimer[4] )
call TriggerRegisterTimerExpireEventBJ( gg_trg_Timer_Expires, AttackTimer[5] )
call TriggerAddAction( gg_trg_Timer_Expires, function TimerExpire )
endfunction
 
Last edited by a moderator:
Level 4
Joined
Mar 20, 2014
Messages
67
I get it it's inefficient, I'd like to fix everything and make it work before I want to make it efficient. It's displaying the correct players and the timer shows and starts. But it doesn't trigger the expire. The actions in it don't trigger, I put some debugs in it but they don't show
 
Events aren't dynamic enough to deal with variable input. It only reads the value at that point in time and then it'll use it for the event. As such, AttackTimer[0], [1], [2]... [5] should all be set to timers before you register the expire.

As such, remove the "set AttackTimer = CreateTimer()" and just create them before you register the event for them (I used a loop to reduce the amount of code):
JASS:
function InitTrig_Timer_Expires takes nothing returns nothing
    local integer i = 0
    set gg_trg_Timer_Expires = CreateTrigger() 
    loop
        exitwhen i == 6
        set AttackTimer[i] = CreateTimer()
        call TriggerRegisterTimerExpireEventBJ(gg_trg_Timer_Expires, AttackTimer[i])
        set i = i + 1
    endloop
    call TriggerAddAction( gg_trg_Timer_Expires, function TimerExpire )
endfunction

Here is a quick visual diagram that may help:
attachment.php

When you input a variable into a function, it doesn't actually pass the variable. It passes its value. As such, AttackTimer[0] is null on initialization. Because of that, the trigger function will just use "null" as the timer to check expiration for, and it will remain that way, even if you change what AttackTimer points to. It stores only the value at that point in time, so reassigning the variable later on won't do anything for that particular event (you would have to register a new event).

An easier example to understand would be registering when a unit dies. Let's say you have a variable, "Footman" that points to a footman on the map. You register a trigger to check if it dies. If you change the variable "Footman" to point to a knight, what do you think will happen?
The trigger will still only fire when the footman dies. Again, this shows how variables are simply "pointers". They "point" to particular objects, and we only care about the objects they point to--not the variables themselves.


Hopefully that should clear things up, and perhaps it might even fix your problem.
 

Attachments

  • Diagram.png
    Diagram.png
    20.8 KB · Views: 202
Status
Not open for further replies.
Top