FPlatformProcess::Sleep crash in thread - bad thread use ? (flicker effect on damage)

Hi,

I call a function called


twinkleDamage

when my enemy is touched.

I tried many different AsyncTask(ENamedThreads::XXX)
and it seems that more prioritized is the thread, the more the game freeze when the thread is called.

VisualStudio break point when FPlatformProcess::Sleep is called


void AMyBasicMob::twinkleDamage()
{

		AsyncTask(ENamedThreads::AnyBackgroundHiPriTask , [this]() // GAMETHREAD ???
		{

			int i = 0;
			bool val = false;
			auto _mesh = this->GetMesh();
			if (_mesh) {
				while (i < 14) {
					UE_LOG(LogClass, Warning, TEXT("FLICKER LOOP"));

					if (!val) {
						_mesh->SetMaterial(0, FlikkerMaterial);
					}
					else {
						_mesh->SetMaterial(0, BaseMaterial);
					}

                                       // HERE ISSUE
                                       // trigger breakpoint or crash
					FPlatformProcess::Sleep(0.03);
                                        ////////////////
                                       ///////////
					val = !val;
					i++;
				}
				_mesh->SetMaterial(0, BaseMaterial);
				this->hitable = true;

			}
		});
	
}


As you probably guest, I’m a newbie in UE4, but I’ve done many c++ projects by the past,
So somebody can tell me what’s wrong here?

Or If you have a best practice for thread that handle visual modification of actor, let me know!

thanks :slight_smile:

It is likely the calls you are making from inside the thread are not meant to be made outside of the game thread. I am not even sure logging is safe from a non-game thread! Safest position I think is not to make any engine call from a non-game thread, only handle logic there then send messages to the game thread so code there can actually call the engine. However if an UE expert could elaborate on this I would also be interested.