Is possible to set timers in a loop?

Cafo95
Mon, 07 Dec 2020 03:31:14 +0000
Audio

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

3 posts - 1 participant