# Is possible to set timers in a loop?

**URL:** <https://forums.unrealengine.com/t/is-possible-to-set-timers-in-a-loop/475527>\
**Category:** Audio\
**Tags:** timers, timer, unreal-engine, loop, question, set-timer\
**Created:** [December 7, 2020, 3:31am UTC](https://forums.unrealengine.com/t/is-possible-to-set-timers-in-a-loop/475527 "2020-12-07T03:31:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cafo95](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cafo95](https://forums.unrealengine.com/u/Cafo95)\
**Post date:** [December 7, 2020, 3:31am UTC](https://forums.unrealengine.com/t/is-possible-to-set-timers-in-a-loop/475527/1 "2020-12-07T03:31:14Z")

</div>

Hi guys,  
I need to do a for loop for a combo attack, and every attack I need the pawn to wait a random number of seconds.  
It’s the first time I work with Timers so I just copied an example code I’ve found online but it doesn’t work like I want.  
It does before all the timers and then all the attacks.  
I don’t know if it’s possible, I usually do this in Behaviour Tree.  
I’ll just post the code since is very simple to understand:

```
void AMyAIController::DelayPerHit()
{
    int32 HitsToDo = FMath::RandRange(PossessedSword->MinHits, PossessedSword->MaxHits);

    for (int32 i = 0; i < HitsToDo; i++)
    {
        float TimeToWait = FMath::RandRange(PossessedSword->MinDelayForHit, PossessedSword->MaxDelayForHit);
        
        FTimerHandle TimerHandle;
        GetWorldTimerManager().SetTimer(TimerHandle, this, &AMyAIController::Attack, TimeToWait, false);
    }
}

void AMyAIController::Attack()
{
    UE_LOG(LogTemp, Warning, TEXT("Attack"));
}
```

---

<div class="post-metadata">

**Author:** ![Cafo95](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Cafo95](https://forums.unrealengine.com/u/Cafo95)\
**Post date:** [December 7, 2020, 4:34pm UTC](https://forums.unrealengine.com/t/is-possible-to-set-timers-in-a-loop/475527/2 "2020-12-07T16:34:26Z")

</div>

Ok, found the solution, if anyone is interested simply add the times every loop 😉

```
float RandomTimeToWait = FMath::RandRange(PossessedSword->MinDelayForHit, PossessedSword->MaxDelayForHit);
    
TimeToWait += RandomTimeToWait ;
    
GetWorldTimerManager().SetTimer(TimerHandle, this, &AMyAIController::Attack, TimeToWait , false);
```

---

<div class="post-metadata">

**Author:** ![SuryaPrakashModi](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SuryaPrakashModi](https://forums.unrealengine.com/u/SuryaPrakashModi)\
**Post date:** [May 21, 2021, 8:04am UTC](https://forums.unrealengine.com/t/is-possible-to-set-timers-in-a-loop/475527/3 "2021-05-21T08:04:56Z")

</div>

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/b/3/ab3b08d0b3e483c5f9c2cfb15b43ac9f7173a94a.jpeg "How to use Set Timer And Clear Timer in C++ Unreal Engine") ](https://www.youtube.com/watch?v=Asw98YKAOtg)

watch this
