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"

How to execute code on game thread, using AsyncTask or something like that?

So I am working on a plugin which uses mutlithreading, the problem is: I need to debug some stuff on the game thread. This is what I tried:

 #include "Async.h"
 
 ...    
 TArray<FCoverGeneration_CoverStruct>& CoverPoints = Covers;
 AActor* ActorToSaveTo = This;
 AsyncTask(ENamedThreads::GameThread, [CoverPoints, ActorToSaveTo]()
     {    
         ACoverGeneration_BasicCoverActor* Cover = Cast<ACoverGeneration_BasicCoverActor>(ActorToSaveTo);
         if (Cover != NULL)
         {
             Cover->Covers.Append(CoverPoints);
             Cover->DebugVectors();
         }
         else
         {
             UE_LOG(LogTemp, Warning, TEXT("Cast to BasicCoverActor failed!"));
         }
     });

It compiles, but once the code gets to here the engine crashes.

Does anyone know what I am doing wrong, thanks in advance.

-Erik

Product Version: UE 4.13
Tags:
more ▼

asked Jan 13 '17 at 02:24 PM in C++ Programming

avatar image

erik321123
413 17 15 31

avatar image erik321123 Jan 15 '17 at 11:18 AM

anyone who knows this?

avatar image project.gheist Jan 15 '17 at 11:35 AM
 TArray<FCoverGeneration_CoverStruct>& CoverPoints = Covers;

It might be that you are passing through a reference to that array instead of a copy. Try doing the following and see if that fixes your issue;

 TArray<FCoverGeneration_CoverStruct> CoverPoints = Covers;
avatar image erik321123 Jan 15 '17 at 11:50 AM

I have just tried that, unfortunatly that doens't work. Thanks for your reply though.

avatar image erik321123 Jan 15 '17 at 11:56 AM

Also I have tried commenting everything out in the ASyncTask lambda and now it doens't crash, so it is in the lines:

     AsyncTask(ENamedThreads::GameThread, [CoverPoints, ActorToSaveTo]()
     {    
         ACoverGeneration_BasicCoverActor* Cover = Cast<ACoverGeneration_BasicCoverActor>(ActorToSaveTo);
         if (Cover != NULL)
         {
             Cover->Covers.Append(CoverPoints);
             Cover->DebugVectors();
         }
         else
         {
             UE_LOG(LogTemp, Warning, TEXT("Cast to BasicCoverActor failed!"));
         }
     });
avatar image erik321123 Jan 15 '17 at 02:54 PM

After more research, it sems that the cast causes the crash, but I don't know why.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

I have a way to properly execute code on the gamethread:

 #include "Async.h"
 
 1.     AsyncTask(ENamedThreads::GameThread, [=]()
 2.     {    
 PushToActor(This, Covers);
 13.     });
 
 
 void AGeneratCoversThread::PushToActor(AActor* ActorToSaveTo, TArray<FCoverGeneration_CoverStruct> Coverpoints)
 {
 3.         ACoverGeneration_BasicCoverActor* Cover = Cast<ACoverGeneration_BasicCoverActor>(ActorToSaveTo);
 4.         if (Cover != NULL)
 5.         {
 6.             Cover->Covers.Append(CoverPoints);
 7.             Cover->DebugVectors();
 8.         }
 9.         else
 10.         {
 11.             UE_LOG(LogTemp, Warning, TEXT("Cast to BasicCoverActor failed!"));
 12.         }
 }

-Erik

more ▼

answered Jan 18 '17 at 11:08 AM

avatar image

erik321123
413 17 15 31

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

You are running into an assert during the cast. You'll probably break at check(IsInGameThread()); if you attach a debugger.

Multi-Threading: What Not to Do

more ▼

answered Jan 17 '17 at 09:13 PM

avatar image

nathansizemore
92 4 8 13

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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