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
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

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

Source code

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

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