Execute code on GameThread

How to execute code on the GameThread?
I’ve tried this:

FGraphEventRef RewardedVideoCompletedTask = FFunctionGraphTask::CreateAndDispatchWhenReady([&]()
    {
        AMyGameGameMode* GameMode = UMyGameBlueprintLibrary::GetCRGameMode(this);
        GameMode->SaveMeFreeCompleted();
    }, TStatId(), NULL, ENamedThreads::GameThread);
    FTaskGraphInterface::Get().WaitUntilTaskCompletes(RewardedVideoCompletedTask);

but it froze my game… Please help me.

1 Like

I recently added AsyncTask() that should help with that kind of stuff. I tend to use it in combination with a lambda:

AsyncTask(ENamedThreads::GameThread, []() {
	// code to execute on game thread here
});
4 Likes

Which header I need to include?

#include “Async.h”

I’ve included Async.h and call AsyncTask() as you described above and I receive compilation error:
error C3861: ‘AsyncTask’: identifier not found

What version of the Engine are you using? AsyncTask is relatively new - I’m not sure if it’s already in 4.9. You may have to use the code from the Master branch. You should be able to find out by opening Async.h - the declaration of AsyncTask() should be at the very bottom.

No, 4.9 doesn’t have it :frowning:

4.10 Preview 1 also not :frowning:

You can just implement it yourself in your own project for now. The code is really simple. Here is the declaration and the implementation. As you can see, it’s just a convenience wrapper around the TaskGraph API.

I think I need to make use of this. Any more info on how to use it?

I am getting a compilation error.

	AsyncTask(ENamedThreads::GameThread, []() {
		UE_LOG(LogSteamworks, Log, TEXT("Running inside AsyncTask()"));
		UE_LOG(LogSteamworks, Log, TEXT("Calling PublicOnSteamOverlayIsActive from SteamManager"));
		MusicalRangeGameInstance->PublicOnSteamOverlayIsActive(isCurrentOverlayActive);
		UE_LOG(LogSteamworks, Log, TEXT("Exiting AsyncTask()"));
	});

Error

2>D:\Dropbox\MusicalRange-4.14\Source\MusicalRange\SteamManager.cpp(39): error C4573: the usage of 'USteamManager::MusicalRangeGameInstance' requires the compiler to capture 'this' but the current default capture mode does not allow it
2>D:\Dropbox\MusicalRange-4.14\Source\MusicalRange\SteamManager.cpp(39): error C3493: 'isCurrentOverlayActive' cannot be implicitly captured because no default capture mode has been specified

VS notification:

 An Enclosing-funciton local variable cannot be references in a lambda body unless it is in the capture list.

Thanks,

Yes you need to Capture the Variables you use inside your Lambda. Take a look here to fully understand how to use Lambdas http://en.cppreference.com/w/cpp/language/lambda

AsyncTask(ENamedThreads::GameThread, this {