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
本文原创,不定时更新
可以随意转载到任何网站
~但是~ 转载也要按“基本法”
请注明原文出处和作者