How to do something in the game asynchronously

For me on level startup, I randomly generate an environment. My level is empty and there’s an actor that runs this process in it’s BeginPlay method and then destroys itself.

This takes a while and the game is completely frozen for some number of seconds. I want to have a loading screen show up while this is happening and have the game still be interactable. I’m guessing I need to have stuff happen on a separate thread somehow.

Have a look into FQueuedThreadPool and FQueuedWork, that’s the way some other people on the forum and I are generating our terrain in seperate threads :slight_smile:
There’s not that much documentation about it but with a little research you should be able to find out how to use it.

I was able to find this article:

Unfortunately, it specifically mentions not to create UObjects or anything like that in the thread, which is exactly what my random level generator does.

I found that there is a MoviePlayer class that’s used for loading screens. I’m trying to figure that out now. I’m going to start a separate thread for that since it’s a completely different topic.

Are UObjects only the final results or are you using them also for intermediate results? If they are only used to store final results, you could collect spawning parameters in a struct and add that to a list for the main thread to process.

I presiously used the methode you posted as well but it was about nine times slowlier than the approach with FQueuedThreadPool and FQueuedThreads :wink:

Regarding the UObjects: I don’t know how you are handling it but I’m creating the Uobjects all at once in the main thread and then pass only the data into the WorkerThreads, like NisshokuZK already mentioned.

I do have spawning parameter structs for some things, but I create UActors on the fly so I can perform collision checks against existing areas so newly spawning areas won’t intersect them. I’m pretty sure that ends up using the physics engine for overlap tests. I can’t think of any other way to do that, unless I write my own collision checking code that doesn’t use the built in physics engine which would be overkill.