How to run code on game thread from a BlueprintFunctionLibrary

My game is created using Blueprints but I had to create a BlueprintFunctionLibrary for the part of the logic that handles player information with the server (like username, password…). Inside the function I’m using JNI as I already have a really good server that handles different function as well as relation with the DB. When I’m running the game from UE it is running fine (did 700+ function call tests, every time it worked as it should), but the problem is that as soon as I create a shipping form of the game, it stop working. From what I understand from other thread, a possible problem is that the part that fails should run on the game thread. So I want to at least give this suggestion a try and run it there.

What do I have to use inside the function so that the code runs on game thread? And how do I tell it to wait for that code to finish to avoid other problems that may appear if this code runs later then some other stuff that depend on it.

This will also help me in the future to learn how to use it, in case there are situation in which running code in game thread is the only way to do it.

Simple BlueprintFunctionLibrary .h file content

class MOREFUNTOGETHER_API UTestSocket : public UBlueprintFunctionLibrary
		UFUNCTION(BlueprintCallable, Category = "MyCategory")
		static int testFunction();


int UTest::testFunction() {
// Code that should run on game thread, which return a number as a test, and the rest of the execution waits until "testFunction" finishes

Just as a note for anyone who wanders what is it that I’m using, I’m using JNI inside the testFunction that has the Socket with the server, and the problem is when I’m calling the function FindClass, it returns nullptr in shipping form, and not while running from editor. And on some threads not related to UE they were saying that a problem might be that the function isn’t running on the main thread, which from what I understand that is game thread in UE.

Blueprint code always runs on the game thread.

Have you actually verified that the problem is that the code isn’t running on the main thread?
And if you have, what is the call stack that causes that to happen?
If you call into Java, and use the Java VM threads, then you may need to marshal any callback you get from there, which is code you write outside the direct blueprint function calls.
You can use something like FFunctionGraphTask::CreateAndDispatchWhenReady() to queue the call to execute on the main thread when available.


Like I said, when I run the game from UE editor or launch it via ProjectName.uproject it is running fine, but when I create the shipping form, then it fails. This is why I thought that the problem might be a thread problem, that maybe threads are handled different in shipping.
Looking at why FindClass might fail, the thread issue was the only one I haven’t tried, as the location is correct because I’m setting the text of an element in UI with the path send there, and it is correct, and the thread part I haven’t tested as I don’t know how to do it.

Unless there’s literally an assert saying “you’re doing this from not-the-main-thread,” then it’s very unlikely to be a threading difference.

It’s much more likely that some class doesn’t get correctly identified as needed and included in the output package. You may need to list it in the manifest of things to include in the packed build.
Also, it’s totally possible to debug a packaged build, too – attach the debugger once you’re hitting the crash/assert, and see what’s going on.