Hi everyone! I’m trying to use the Delay function in the KismetSystemLibrary but it doesn’t seem to work despite me providing all the proper functions parameters. Has anyone been able to get this to work? I’d rather not have to create separate timers for it since there’s obviously a function already made for delays.
// Along with other includes
// Various code
UKismetSystemLibrary::Delay(this, 3.0f, LatentInfo);
UKismetSystemLibrary::PrintString(this, FString("After Delay"));
The Delay function takes a world context, a float duration, and a latent action info struct.
Doing this exact same thing in blueprints works, but here, the PrintString function fires immediately.
Since blueprint-like delay usage would block execution on main-thread, you don’t have this option on C++. If you are doing this for syntax reasons, you should look at using Lambda functions with timers, it looks same as your code but more wrapped in brackets.
Have a nice day.
Delays are considered latent functions in unreal terminology. Basically they should not block the main execution thread and run aside it as a seperate thread.
I suppose unreal only supports them in Blueprint editor via Events. The engine has a complex mechanism that converts Events to threads whenever they include a latent function node (like delay) in their call graph.
The Events created in unreal blueprint editor (red nodes) do block the main execution thread unless they include a latent function like the Delay node in them. So they are no different then Function without a return value unless they include latent nodes.
If they include latent nodes however suddenly the magic happens and they become threads.
On the contrary Functions created in blueprint editor (purple nodes) will always block the main execution thread no matter what.
Check my own question about this issue which I eventually answered myself:
So the code you have called in your C++ method is designed to be called from the Editor not from C++ side.
To achieve the same effect on C++ side the easier solution I believe is to use timers or your own thread implementation (see FRunnable).
The easiest solution however (though not performance friendly) is to expose a function to the unreal editor (BlueprintImplementableEvent or BlueprintNativeEvent) and override it so that it uses your Delay node. Whenever you need a delay you can call this blueprint function from C++ and when it time outs it can return back to the C++ code through a call to a C++ implemented BlueprintCallable function.