• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【UE4C++】Tick的三种方式、异步蓝图节点、LatentAction

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  • 包括
    • 默认 Tick (Actor、Component、UMG)
    • TimerManager 定时器
    • FTickableGameObject
      • 可以写原生 Object
      • 也可以继承UObject 使用
  • 下面利用 AActor 直接实现三种 Tick
class  FTickableObject :public FTickableGameObject
{
public:
	bool bEnableTick = false;
	int32 TickCounter = 0;

	virtual void Tick(float DeltaTime)override {
		UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"\t [TickCounter]%d"), TickCounter);
	}
	virtual bool IsTickable()const override { return bEnableTick; }
	virtual TStatId GetStatId() const override { RETURN_QUICK_DECLARE_CYCLE_STAT(UTickableObject, STATGROUP_Tickables); }
};

UCLASS()
class DESIGNPATTERNS_API AAsyncTickActor : public AActor
{
	GENERATED_BODY()
public:	
	// Sets default values for this actor's properties
	AAsyncTickActor();

	// Called every frame
	virtual void Tick(float DeltaTime) override;
	void TimerTick();
protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

private:
	int32 TickCounter = 0;
	FTimerHandle TimeHandle;
	TSharedPtr<FTickableObject> TickableObject;
};
AAsyncTickActor::AAsyncTickActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;	
}

// Called when the game starts or when spawned
void AAsyncTickActor::BeginPlay()
{
	Super::BeginPlay();
	TickableObject = MakeShareable(new FTickableObject());
	TickableObject->bEnableTick = true;
	GetWorld()->GetTimerManager().SetTimer(TimeHandle, this, &AAsyncTickActor::TimerTick, 0.1f, true);
}

// Called every frame
void AAsyncTickActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"\t[TickCounter]%d"), TickCounter);
	if (TickableObject.IsValid())
	{
		TickableObject->TickCounter = TickCounter;
	}

}

void AAsyncTickActor::TimerTick()
{
	TickCounter++;
	UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"%\t [TickCounter]%d"), TickCounter);
}

异步蓝图节点

使用方法

  • 继承 UBlueprintAsyncActionBase

    • 成员函数 Activate 用于内部触发委托绑定的事件
    • 成员函数 RegisterWithGameInstance 允许将该对象注册到 GameInstance,防止GC
    • 注册后,调用成员方法 SetReadyToDestroy 销毁、释放,从 GameInstance 注销
    • 源码
      class UGameInstance;
      
      UCLASS()
      class ENGINE_API UBlueprintAsyncActionBase : public UObject
      {
      	GENERATED_UCLASS_BODY()
      
      	/** Called to trigger the action once the delegates have been bound */
      	UFUNCTION(BlueprintCallable, meta=(BlueprintInternalUseOnly="true"))
      	virtual void Activate();
      
      	/**
      	 * Call to globally register this object with a game instance, it will not be destroyed until SetReadyToDestroy is called
      	 * This allows having an action stay alive until SetReadyToDestroy is manually called, allowing it to be used inside loops or if the calling BP goes away
      	 */
      	virtual void RegisterWithGameInstance(UObject* WorldContextObject);
      	virtual void RegisterWithGameInstance(UGameInstance* GameInstance);
      
      	/** Call when the action is completely done, this makes the action free to delete, and will unregister it with the game instance */
      	virtual void SetReadyToDestroy();
      
      protected:
      	TWeakObjectPtr<UGameInstance> RegisteredWithGameInstance;
      };
      
  • 声明静态函数

  • 需要动态多播委托,作为多个输出节点

代码实现

  • 代码

    UCLASS()
    class DESIGNPATTERNS_API AAsyncTickActor : public AActor
    {
    public:
    	UPROPERTY(BlueprintReadWrite)
    		TArray<int32>CountdownNums;
    };
    
    // 定义委托类型
    DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FCompleteHandleDelegate, int32, Result);
    
    UCLASS()
    class DESIGNPATTERNS_API UMyBPAsyncAction : public UBlueprintAsyncActionBase
    {
    	GENERATED_BODY()
    public:
    	UMyBPAsyncAction() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"")); }
    	~UMyBPAsyncAction() { UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__"")); }
    
    	// 自定义的异步蓝图节点
    	UFUNCTION(BlueprintCallable, meta = (WorldContext = "WorldContextObject"), Category = "MyBPAsyncAction")
    		static UMyBPAsyncAction* AsyncCountdown(UObject* WorldContextObject, AAsyncTickActor* AsyncTickActor, int32 StartNum);
    public:
    	//输出节点
    	UPROPERTY(BlueprintAssignable)
    		FCompleteHandleDelegate OnSucceeded;
    	
    	//输出节点
    	UPROPERTY(BlueprintAssignable)
    		FCompleteHandleDelegate OnFailed;
    private:
    	FTimerHandle TimerHandle;
    	
    protected:
    	virtual void Activate() override;
    };
    
    void UMyBPAsyncAction::Activate()
    {
    	// 开新的线程,测试 Activate 调用情况
    	Async(EAsyncExecution::ThreadPool, [&]()
    		{
    			UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Start Delay 1s."));
    			FPlatformProcess::Sleep(1.0f);
    			UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Delay Finished."));
    		});
    }
    
    UMyBPAsyncAction* UMyBPAsyncAction::AsyncCountdown(UObject* WorldContextObject, AAsyncTickActor* AsyncTickActor, int32 StartNum)
    {
    	if (WorldContextObject == nullptr)
    	{
    		FFrame::KismetExecutionMessage(TEXT("Invalid WorldContextObject"), ELogVerbosity::Error);
    		UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Invalid WorldContextObject."));
    		return nullptr;
    	}
    
    	UMyBPAsyncAction* MyBPAsyncActionNode = NewObject<UMyBPAsyncAction>();
    	// Lambda 表达式
    	auto CountdownFunc = [&,MyBPAsyncActionNode, WorldContextObject, AsyncTickActor, StartNum]() {
    		if (IsValid(AsyncTickActor))
    		{
    			if (AsyncTickActor->CountdownNums.Num() == StartNum)
    			{
    				// OnSucceeded输出节点
    				MyBPAsyncActionNode->OnSucceeded.Broadcast(1);	
    				
    				// 清空定时器
    				WorldContextObject->GetWorld()->GetTimerManager().ClearTimer(MyBPAsyncActionNode->TimerHandle);
    				
    				// 如果不使用,则销毁
    				MyBPAsyncActionNode->SetReadyToDestroy();
    				UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Countdown Completed."));
    			}
    			else
    			{
    				int32 length = AsyncTickActor->CountdownNums.Num();
    				AsyncTickActor->CountdownNums.Add(length + 1);
    				UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Counting down... %d"), length + 1);
    			}
    		}
    		else
    		{
    			// OnFailed 输出节点
    			MyBPAsyncActionNode->OnFailed.Broadcast(-1);
    			WorldContextObject->GetWorld()->GetTimerManager().ClearTimer(MyBPAsyncActionNode->TimerHandle);
    			UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Invalid AsyncTickActor."));
    			FFrame::KismetExecutionMessage(TEXT("Invalid AsyncTickActor"), ELogVerbosity::Error);
    		}
    	};
    	
    	// 设置定时器并开始
    	WorldContextObject->GetWorld()->GetTimerManager().SetTimer(MyBPAsyncActionNode->TimerHandle, FTimerDelegate::CreateLambda(CountdownFunc), 1.0f, true);
    	UE_LOG(LogTemp, Warning, TEXT(__FUNCTION__" Countdown Starting."));
    	return MyBPAsyncActionNode;
    }
    


Latent Action


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
(iphone)Objectivechessian发布时间:2022-07-14
下一篇:
VisualStudio2015&amp;C#6.0试用报告,持续更新。发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap