UE4筆記-異步藍圖節點 (UBlueprintAsyncActionBase)


記錄備查:

基於的UBlueprintAsyncActionBase類可實現異步藍圖節點

-------------------------------------------------------------------------------

例子:

.h

UCLASS()
class MYPROJECT_API UBPAsyncNode_Custom : public UBlueprintAsyncActionBase
{
    GENERATED_BODY()
public:
    DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FBPPin_Result, const TArray<FString>&, Datas);
    /** 輸出針腳1 */
    UPROPERTY(BlueprintAssignable)
        FBPPin_Result OnSuccess;

    /** 輸出針腳2 */
    UPROPERTY(BlueprintAssignable)
        FBPPin_Result OnFailure;
public:
    /** 藍圖節點:負責NewOBject 藍圖節點(創建工廠模式) */
    UFUNCTION(BlueprintCallable, meta = (BlueprintInternalUseOnly = "true", WorldContext = "WorldContextObject", Category = "Biz API"))
        static UBPAsyncNode_Custom* CustomAsyncBPNode(UObject* WorldContextObject);
protected:
    // UBlueprintAsyncActionBase interface


    /** 程序通過(運行)節點是調用 */
    virtual void Activate() override;

    //~UBlueprintAsyncActionBase interface

};

 

.cpp

UBPAsyncNode_Custom* UBPAsyncNode_Custom::CustomAsyncBPNode(UObject* WorldContextObject)
{
    UBPAsyncNode_Custom* Ins = NewObject<UBPAsyncNode_Custom>();
    return Ins;
}

void UBPAsyncNode_Custom::Activate()
{
    //測試邏輯----開新線程,延遲一秒后隨機調用成功或失敗委托
    Async(EAsyncExecution::ThreadPool, [&]()
        {
            FPlatformProcess::Sleep(1.0f);
            int32 result = FMath::RandRange(1,100);

            TArray<FString> arr;
            if (result > 50)
            {
                this->OnSuccess.Broadcast(arr);
            }
            else 
            {
                this->OnFailure.Broadcast(arr);
            }
        });
}

 

使用:

 

 

Note:

容器類類結果必須是

const  YourClass & 

如例子的TArray容器

 DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FBPPin_Result, const TArray<FString>&, Datas);

否則報錯

Signature Error: XXXX  does not match the necessary signature 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM