Skip to content

Instantly share code, notes, and snippets.

@unktomi
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unktomi/9d95a254287fef197b01 to your computer and use it in GitHub Desktop.
Save unktomi/9d95a254287fef197b01 to your computer and use it in GitHub Desktop.
/**
* One-shot continuation built on top of FPendingLatentAction
*/
template <class T> class FContinueAction : public FPendingLatentAction
{
bool Called;
const FName ExecutionFunction;
const int32 OutputLink;
const FWeakObjectPtr CallbackTarget;
T &Result;
public:
virtual void Call(const T &Value) {
Result = Value;
Called = true;
}
FContinueAction(T& ResultParam, const FLatentActionInfo& LatentInfo) :
Called(false)
, Result(ResultParam)
, ExecutionFunction(LatentInfo.ExecutionFunction)
, OutputLink(LatentInfo.Linkage)
, CallbackTarget(LatentInfo.CallbackTarget)
{
}
virtual void UpdateOperation(FLatentResponse& Response) override
{
Response.FinishAndTriggerIf(Called, ExecutionFunction, OutputLink, CallbackTarget);
}
};
// Pseudocode Example of use in a modified MediaPlayer that handles operations asynchronously
UFUNCTION(BlueprintCallable, Category = "Media|MediaPlayer", meta = (Latent, WorldContext = "WorldContextObject", LatentInfo = "LatentInfo"))
virtual void OpenUrlLatently(const FString& Url, bool &Result, UObject* WorldContextObject, struct FLatentActionInfo LatentInfo)
{
FContinueAction<bool> *ContinueAction = 0;
if (UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject))
{
FLatentActionManager& LatentActionManager = World->GetLatentActionManager();
if (LatentActionManager.FindExistingAction<FContinueAction<UVaRestJsonObject>>(LatentInfo.CallbackTarget, LatentInfo.UUID) == NULL)
{
LatentActionManager.AddNewAction(LatentInfo.CallbackTarget, LatentInfo.UUID, ContinueAction = new FContinueAction<bool>(Result, LatentInfo));
// Arrange to call ContinueAction asynchronously when the result of OpenUrl is known
DoOpenUrl(Url);
} else {
// handle duplicate
}
} else {
// handle no world
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment