Is possible to set timers in a loop?

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"));
}
1 Like

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

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

watch this

1 Like