4#include "CoreMinimal.h"
5#include "Engine/Engine.h"
6#include "Engine/LatentActionManager.h"
7#include "LatentActions.h"
8#include "UObject/WeakObjectPtr.h"
10template <
class T,
class Arg = const T&>
11class TCallbackAction final :
public FPendingLatentAction
14 TCallbackAction(T& Result,
const FLatentActionInfo& LatentInfo)
15 : ExecutionFunction(LatentInfo.ExecutionFunction), OutputLink(LatentInfo.Linkage), CallbackTarget(LatentInfo.CallbackTarget), Result(Result)
18 TCallbackAction(
const TCallbackAction&) =
delete;
19 TCallbackAction& operator=(
const TCallbackAction&) =
delete;
21 static void CreateLatentAction(
22 const UObject* WorldContextObject,
23 const FLatentActionInfo LatentInfo,
25 TFunctionRef<
void(TFunction<
void(Arg)> Callback)> Action)
27 if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::LogAndReturnNull))
29 FLatentActionManager& LatentActionManager = World->GetLatentActionManager();
30 if (LatentActionManager.FindExistingAction<TCallbackAction<T, Arg>>(LatentInfo.CallbackTarget, LatentInfo.UUID))
35 TCallbackAction<T, Arg>* DelegateAction =
new TCallbackAction(Output, LatentInfo);
37 [DelegateAction](Arg InResult)
39 DelegateAction->Result = InResult;
40 DelegateAction->bDone =
true;
42 LatentActionManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, DelegateAction);
47 virtual void UpdateOperation(FLatentResponse& Response)
override
49 Response.FinishAndTriggerIf(bDone, ExecutionFunction, OutputLink, CallbackTarget);
52 FName ExecutionFunction;
54 FWeakObjectPtr CallbackTarget;