Spawn Actors from Plugin Threads

I am trying to spawn a static mesh actor into the editor from a C++ plugin that I am creating. The call worked great when I was prototyping in a slate UI control callback, but it is crashing now that I moved the logic to a thread in the plugin. Unreal is crashing when I make the following call from a thread in plugin.

AStaticMeshActor *actor = Cast(GEditor->GetEditorWorldContext().World()->SpawnActor(AStaticMeshActor::StaticClass()));

After debugging this in VS, it broke on the following line. check(IsInGameThread());

I am definitely NOT in the game thread. I have read the Multi Threading How To in the Unreal wiki. It definitely warns you not to manipulate UObjects from other threads.

So my question is: how do you accomplish spawning/destroying actors from other threads? There must be a way to do this. This information would be extremely useful to add to the Multi Threading How To wiki.

Thanks for the help!

Don’t want to break the bubble, but UE4 gameplay is essentially single threaded. Anything that touches UObjects must happen in the game thread.

What exactly is your plugin doing that makes you think that it will benefit from multithreading?

Thanks for the reply. My thread is waiting on a FSocket->>Recv() call to receive asynchronous messages from a server. Some message require that I manipulate/spawn UObjects

Hmm, you probably don’t need a separate thread for it.

I’d handle the socket in the game thread by registering a tick function, that checks whether there’s any data to be received and only then calling Recv.

That’s also how the game itself handles multiplayer internally.

Invert interaction make UObject interact with your socket data that you received left in memoery by other thread, maybe on Tick()? OR some other event that needs that data

One note, not complitly single threaded, tick manager has multithread mode which separates tick computations to diffrent worker threads

Thanks for all of the responses. I’ll try these suggestions and post my solution for the next guy.

That will not work in call cases. If the data he is receiving is data which, say moves the character forward, then movement will be tied to the game tick. Since Tick is called once per frame, you are essentially binding your data processing rate to that of the frame rate.

Is there an alternative? What if I want to consume data as soon as it arrives, and not wait for the Tick?