x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

[4.7.6] Why does World->OverlapMulti causes a hang in my FRunnable?

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?

Product Version: UE 4.7
Tags:
more ▼

asked Aug 05 '15 at 04:54 PM in C++ Programming

avatar image

Erdrik
378 45 51 71

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

You can't do calls into the UObject system on other threads. You can only do gameplay from the Game thread.

more ▼

answered Aug 05 '15 at 08:13 PM

avatar image

joeGraf STAFF
1.5k 35 12 67

avatar image Erdrik Aug 05 '15 at 08:42 PM

k. The whole point of this idea was to move AI off of the Game Thread to prevent lag/slow down that could result from lots of AI opponents running at the same time during gameplay.

If I can't move the overlap checks and traces (AFAIK, pretty much the only things expensive enough to be worth moving) whats the point? How am I suppose to manage my AI without crippling the game thread?

Am I just thinking about this wrong?

avatar image joeGraf STAFF Aug 05 '15 at 08:44 PM

You can use async traces and overlaps. The results come back one or more frames later. Async line trace docs

avatar image Erdrik Aug 05 '15 at 09:07 PM

Thank you. Ill explore those.

avatar image ColdSteel48 Apr 24 '18 at 11:32 PM

The link is broken, could you update please? Or the API was removed?

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question