关于 UE4 委托写法

发布时间 2023-05-06 01:20:40作者: 索智源

 

Source\CoinCollector\DelegateActor.h

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #pragma once
 4 
 5 #include "CoreMinimal.h"
 6 #include "GameFramework/Actor.h"
 7 #include "DelegateActor.generated.h"
 8 
 9 // 声明一个没有参数,没有返回值的委托
10 DECLARE_DELEGATE(FNoParamNoReturn);
11 // 带参数的委托,给与参数时只需要指定类型
12 DECLARE_DELEGATE_OneParam(FParam1NoReturn, const FString&);
13 DECLARE_DELEGATE_TwoParams(FParam2NoReturn, const FString&, float);
14 // 带参数,并且有返回值的委托
15 DECLARE_DELEGATE_RetVal_TwoParams(float, FParam2Return, const FString&, float);
16 
17 UCLASS()
18 class COINCOLLECTOR_API ADelegateActor : public AActor
19 {
20     GENERATED_BODY()
21     
22 public:    
23     // Sets default values for this actor's properties
24     ADelegateActor();
25 
26 protected:
27     // Called when the game starts or when spawned
28     virtual void BeginPlay() override;
29 
30 public:    
31     // Called every frame
32     virtual void Tick(float DeltaTime) override;
33 
34 public:
35     FNoParamNoReturn DelegateNoParamNoReturn;            // 使用声明过的委托名称定义一个委托对象
36     FParam1NoReturn DelegateOneParamNoReturn;
37     FParam2NoReturn DelegateTwoParamNoReturn;
38     FParam2Return DelegateTwoParamReturn;
39 
40 private:
41     // 没有参数和返回值的委托函数
42     void TestNoParamNoReturn();        
43     // 带一个参数和没有返回值的委托函数
44     void TestOneParamNoReturn(const FString&);
45     // 带两个参数和没有返回值的委托函数
46     void TestTwoParamNoReturn(const FString&, float);
47     // 带两个参数和 Float 返回值的委托函数
48     float TestTwoParamReturn(const FString&, float);
49 
50 private:
51     float TimerMax = 3.f;
52     float TimerCount = 0;
53 };

 

Source\CoinCollector\DelegateActor.cpp

 1 // Fill out your copyright notice in the Description page of Project Settings.
 2 
 3 #include "DelegateActor.h"
 4 #include "MoveActor.h"
 5 #include "NewTypes.h"
 6 
 7 // Sets default values
 8 ADelegateActor::ADelegateActor()
 9 {
10      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
11     PrimaryActorTick.bCanEverTick = true;
12 }
13 
14 // Called when the game starts or when spawned
15 void ADelegateActor::BeginPlay()
16 {
17     Super::BeginPlay();
18 
19     // 对于我们需要执行的委托对象,我们需要给他指定存在的真实函数(绑定委托)
20     DelegateNoParamNoReturn.BindUObject(this, &ADelegateActor::TestNoParamNoReturn);
21     DelegateOneParamNoReturn.BindUObject(this, &ADelegateActor::TestOneParamNoReturn);
22     DelegateTwoParamNoReturn.BindUObject(this, &ADelegateActor::TestTwoParamNoReturn);
23     DelegateTwoParamReturn.BindUObject(this, &ADelegateActor::TestTwoParamReturn);
24 }
25 
26 // Called every frame
27 void ADelegateActor::Tick(float DeltaTime)
28 {
29     Super::Tick(DeltaTime);
30 
31     if (TimerCount >= TimerMax)
32     {
33         DelegateNoParamNoReturn.ExecuteIfBound();                   // 执行委托
34         DelegateOneParamNoReturn.ExecuteIfBound(TEXT("ABC"));
35         DelegateTwoParamNoReturn.ExecuteIfBound(TEXT("ABC"), TimerCount);        // ExecuteIfBound 只能是执行没有返回值的委托
36         if (DelegateTwoParamReturn.IsBound())
37         {
38             TimerCount = DelegateTwoParamReturn.Execute(FString("CDE"),TimerCount);    // 执行有返回值的委托
39         }
40         else
41         {
42             UE_LOG(SZYLog, Display, TEXT(" DelegateTwoParamReturn.IsBound() == NULL "));
43         }
44         UE_LOG(SZYLog, Display, TEXT("TimerCount: %f"), TimerCount);
45         UE_LOG(SZYLog, Display, TEXT(" ===================== "));
46         TimerCount = 0;
47     }
48     TimerCount += DeltaTime;
49 }
50 
51 void ADelegateActor::TestNoParamNoReturn()
52 {
53     UE_LOG(SZYLog, Display, TEXT("TestNoParamNoReturn Go!!!"));
54 }
55 
56 void ADelegateActor::TestOneParamNoReturn(const FString& Param)
57 {
58     UE_LOG(SZYLog, Display, TEXT("TestNoParamNoReturn Go!!! FString: %s"), *Param);
59 }
60 
61 void ADelegateActor::TestTwoParamNoReturn(const FString& Param1, float Param2)
62 {
63     UE_LOG(SZYLog, Display, TEXT("TestTwoParamNoReturn Go!!! param1: %s, param2: %f"), *Param1, Param2);
64 }
65 
66 float ADelegateActor::TestTwoParamReturn(const FString& Param1, float Param2)
67 {
68     static float Tmp = 0;
69     Tmp += Param2;
70     UE_LOG(SZYLog, Display, TEXT("TestTwoParamReturn Go!!! param1: %s, param2: %f"), *Param1, Param2);
71     if (Tmp >= 30.f)
72     {
73         // 解委托
74         DelegateTwoParamReturn.Unbind();
75     }
76     return Tmp;
77 }