I have a box component that when it over laps with an AI Class, damage will be dealt over time to that AI class, using a timer. However damage is only dealt to one AI class. So if I am overlapping with two AI only one will be damaged. Though I have noticed that initially both AI take damage, but once the timer starts only one AI takes damage. Hope that makes sense, thanks.
Code:
void AC_Ability2::OnOverlapBegin(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
UWorld* World = GetWorld();
for (TActorIterator<AC_BaseAI> It(World, AC_BaseAI::StaticClass()); It; ++It)
{
BaseAI = *It;
if (BaseAI != NULL)
{
BaseAIArray.Add(BaseAI);
BaseAI = Cast<AC_BaseAI>(*It);
if (OtherActor == BaseAI && OtherComp == BaseAI->BoxComp)
{
ApplyDamage(BaseAI);
}
}
}
}
void AC_Ability2::ApplyDamage(AActor* AIActor)
{
UGameplayStatics::ApplyDamage(AIActor, Ability2DamageAmount, UGameplayStatics::GetPlayerController(this, 0), this, NULL);
TimerDel.BindUFunction(this, FName("ApplyDamage"), AIActor);
GetWorldTimerManager().SetTimer(DamageHandle, TimerDel, Ability2Rate, true);
}
Sorry, this is just a guess and I didn’t understand all, but: do you use one timer for all AIs? In that case, I think, the timer is always overwritten. Check if the continous damage is applied always for the last AI that enters the overlap box.
If you could confirmed that this is the issue: handle the damage within each AI actor. Then each AI has its own timer and it should work.
Furthermore, the source code for OnOverlapBegin looks totally tedious. Just cast OtherActor
to AC_BaseAI
and if that does not give nullptr
, go on.
Yes thank you very much. Is it possible to mark a comment as an answer?
There is only one timer in the class AC_Ability2
, so this timer is overwritten every time it is set new. Thus, only the last AI actor entering the box is considered and getting continous damage.
Handle the damage within each AI actor, i. e. create a method AC_BaseAI::ApplyDamage()
. Then each AI has its own timer and it should work.
No. I wrote an “official” answer so you can mark it.
Hi, thanks for the reply and yes the last AI to overlap with the box is damaged. So how would I implement multiple timers for each AI? and thanks for the advice about the overlap casting, I’m new to C++.
Oh wait I misread what you wrote. I’ll implement the damage in the AI class instead. Thanks