Skip to content

Instantly share code, notes, and snippets.

@ivo-alves
Created May 10, 2023 10:03
Show Gist options
  • Save ivo-alves/aee5e76fdb55515e0a487b209d26447e to your computer and use it in GitHub Desktop.
Save ivo-alves/aee5e76fdb55515e0a487b209d26447e to your computer and use it in GitHub Desktop.
Unreal's built-in task graph system async task (runs on main thread)
// start Foreground worker
FGraphEventRef Task = FFunctionGraphTask::CreateAndDispatchWhenReady([this]() {
AsyncHadronTick();
}, TStatId(), nullptr, ENamedThreads::AnyThread);
void AVehicleTest::AsyncHadronTick()
{
double LastStepTime = FPlatformTime::Seconds();
while (IsHadronPlaying)
{
double StartTime = FPlatformTime::Seconds();
{
TRACE_CPUPROFILER_EVENT_SCOPE(Vehicle::TestFunction::TraceCost);
}
double EndTime = FPlatformTime::Seconds();
double ExecutionTime = EndTime - StartTime;
double TargetTime = 1.0f / 100.0f;
double DelayTime = TargetTime - ExecutionTime;
if (DelayTime > 0)
{
FPlatformProcess::Sleep(DelayTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment