I made a subclass of FRunnable to handle AI calculations.
Im not familiar with Multi Threading, so I was just setting up some tests at first.
I put in a World->OverlapMulti function to get a list of Actors around the AIPawn.
Im using the following setup in Run to call it:
//Run
uint32 ADMAIThreadManager::Run()
{
//Initial wait before starting
FPlatformProcess::Sleep(0.03);
//While not told to stop this thread
// and not yet finished
while (StopTaskCounter.GetValue() == 0 && !IsFinished())
{
if (AIData->GetData()[0].CycleCounter == 1)
{
if (IsIntEven(RunCycleCounter)) //every other cycle count
{
APawn* TempPawn = AIData->GetData()[0].AIPawn;
if (TempPawn)
{
TArray<struct FOverlapResult> Overlaps;
TArray<AActor*> IgnorePawns;
IgnorePawns.Add(TempPawn);
Overlaps = OverlapSphereOnPoint(TempPawn->GetActorLocation(), 10000, IgnorePawns);
AIData->GetData()[0].TargetList.Empty();
for (size_t i = 0; i < Overlaps.Num(); i++)
{
//TODO: add Team recognition
if (Overlaps[i].GetActor()) AIData->GetData()[0].TargetList.Add(Overlaps[i].GetActor());
}
}
}
RunCycleCounter++;
if (RunCycleCounter > 32) RunCycleCounter = 0;
AIData->GetData()[0].CycleCounter = 0;
}
}
return 0;
}
AIData->GetData()[0].CycleCounter is set to 1 in a Timer in the controller, to ensure that the OverlapSphereOnPoint function(that handles the actual World->OverlapMulti call) isn’t called too often.
My cycling code works, and OverlapSphereOnPoint is only called every other second.
But when it does the game hangs.
When testing with “Play in Editor” the hang is excessive, halting the game for several seconds.
If I build the project and test outside of the editor the hang is much less(< a second but still noticeable).
The whole reason I want to multi thread the AI is so it doesn’t lag out the other aspects of the game.
And the weird thing is that if I just put that code into the AI Controller, there wouldn’t be any hang at all.
(I already have other actors with similar and much more frequent uses of World->OverlapMulti that don’t produce any lag…)
Is it because Im trying to call World->OverlapMulti from a different thread?
If so, how am I suppose to achieve these kinds of searches from another thread?