Add asynchronous blueprint nodes (timing nodes) in C++ in UE4

Preface

In the previous article(Link),We have introduced how to build a multi-execution output pin node in C++. This article is just a starting point to explain how to implement asynchronous node construction, and it does not mean that the actual output must be produced according to this solution.

Since in Blueprint, if you want to write asynchronous nodes, you can only use macros (this is not a logic unit) at present. If you have other ways, I hope to tell me.

But it can be done in C++. We found that there are a lot of asynchronous nodes in the blueprint (Delay, AIMoveTo, LoadStreamLevel FindSessions, etc.). To identify the asynchronous nodes, just look for the clock mark in the upper right corner of the blueprint node (Figure 1)

Figure 1

Code

First, if you want to write an asynchronous node in C++, the easiest way is to inherit the UBlueprintAsyncActionBase class (see the previous blog). I have tried to inherit UObject myself, but to no avail. The class relationship should be agreed upon by the engine. If you know the reason, please tell me.

Head file

 1// Fill out your copyright notice in the Description page of Project Settings.
 2 
 3#pragma once
 4 
 5#include "CoreMinimal.h"
 6#include "Kismet/BlueprintAsyncActionBase.h"
 7#include "BlueprintAsyncNode.generated.h"
 8 
 9DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FBlueprintAsyncNodePinResult, int32, Result);
10 
11/**
12 * 
13 */
14UCLASS()
15class UECPP_API UBlueprintAsyncNode : public UBlueprintAsyncActionBase
16{
17    GENERATED_BODY()
18 
19    UPROPERTY(BlueprintAssignable)
20    FBlueprintAsyncNodePinResult OnSuccess;
21 
22    UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true", WorldContext = "WorldContextObject", Delay = "0.5"))
23    static UBlueprintAsyncNode* AsyncDelay(UObject* WorldContextObject, float Delay);
24 
25protected:
26    void TimeoutCallback();
27};

Source code

 1// Fill out your copyright notice in the Description page of Project Settings.
 2 
 3#include "BlueprintAsyncNode.h"
 4#include <Engine/World.h>
 5#include <TimerManager.h>
 6 
 7UBlueprintAsyncNode* UBlueprintAsyncNode::AsyncDelay(UObject* WorldContextObject, float Delay)
 8{
 9    UBlueprintAsyncNode* Node = NewObject<UBlueprintAsyncNode>();
10    //Build a timer
11    FTimerHandle Handle;
12    WorldContextObject->GetWorld()->GetTimerManager().SetTimer(Handle, FTimerDelegate::CreateUObject(Node, &UBlueprintAsyncNode::TimeoutCallback), Delay, false);
13    return Node;
14}
15 
16void UBlueprintAsyncNode::TimeoutCallback()
17{
18    if (OnSuccess.IsBound())
19    {
20        OnSuccess.Broadcast(10);
21    }
22}

This is just a very simple asynchronous logic of a scheduled task. I suggest you take a look at the UBlueprintAsyncActionBase class, which is a class provided by Unreal to us for building asynchronous nodes. UBlueprintAsyncActionBase provides virtual functions and comments, which you can read to help us cope with more development scenario requirements.

I wish you all a happy time playing in UE!

Version V4.21.2

Translations:

Comment