事件处理(Handling Events)和委托(Delegate)代码示例(一)【UE4】【C++】

举报
ShaderJoy 发表于 2021/12/30 01:24:33 2021/12/30
【摘要】 1. 通过重写虚函数来处理事件 MyTriggerVolume.h 自定义一个继承自 Actor 的子类,添加一个 Box 组件作为触发区域,然后通过重写虚函数—— NotifyActorBeginOverlap, NotifyActorEndOverlap 来响应事件 #pragma once #in...

1. 通过重写虚函数来处理事件

MyTriggerVolume.h

自定义一个继承自 Actor 的子类,添加一个 Box 组件作为触发区域,然后通过重写虚函数—— NotifyActorBeginOverlap, NotifyActorEndOverlap 来响应事件


  
  1. #pragma once
  2. #include "GameFramework/Actor.h"
  3. #include "MyTriggerVolume.generated.h"
  4. UCLASS()
  5. class TEST_API AMyTriggerVolume : public AActor
  6. {
  7. GENERATED_BODY()
  8. public:
  9. // Sets default values for this actor's properties
  10. AMyTriggerVolume();
  11. // Called when the game starts or when spawned
  12. virtual void BeginPlay() override;
  13. // Called every frame
  14. virtual void Tick( float DeltaSeconds ) override;
  15. UPROPERTY()
  16. UBoxComponent* TriggerZone;
  17. UFUNCTION()
  18. virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
  19. UFUNCTION()
  20. virtual void NotifyActorEndOverlap(AActor* OtherActor) override;
  21. };

MyTriggerVolume.cpp


  
  1. #include "Test.h"
  2. #include "UE4TestGameMode.h"
  3. #include "MyTriggerVolume.h"
  4. // Sets default values
  5. AMyTriggerVolume::AMyTriggerVolume()
  6. {
  7. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  8. PrimaryActorTick.bCanEverTick = true; ///< 每帧都调用 Tick()
  9. TriggerZone = CreateDefaultSubobject<UBoxComponent>("TriggerZone");
  10. TriggerZone->SetBoxExtent(FVector(200, 200, 100));// 设置触发区域的范围
  11. }
  12. // Called when the game starts or when spawned
  13. void AMyTriggerVolume::BeginPlay()
  14. {
  15. Super::BeginPlay();
  16. }
  17. // Called every frame
  18. void AMyTriggerVolume::Tick( float DeltaTime )
  19. {
  20. Super::Tick( DeltaTime );
  21. }
  22. // 重写虚函数来响应事件
  23. void AMyTriggerVolume::NotifyActorBeginOverlap(AActor* OtherActor)
  24. {
  25. GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, FString::Printf(TEXT("%s entered me"), *(OtherActor->GetName())));// 注意FString::Format需要解引用
  26. }
  27. // 重写虚函数来响应事件
  28. void AMyTriggerVolume::NotifyActorEndOverlap(AActor* OtherActor)
  29. {
  30. GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Red, FString::Printf(TEXT("%s left me"), *(OtherActor->GetName())));
  31. }

2. 绑定在 UFUNCTION 函数上的委托(不带参数)

委托的好处在于,我们不用知道当前指派的函数的细节就可以调用它,它是一种安全版本的函数指针。

以下代码将展示如何关联 UFUNCTION 到一个委托上,即委托执行时,UFUNCTION 将被调用。
(效果为 当玩家进入触发区域,点光源亮)

首先在 UE4TestGameMode.h 中添加一个委托声明(在 UCLASS 之前),如下:

DECLARE_DELEGATE(FStandardDelegateSignature)
 

然后,为 UE4TestGameMode 类添加一个新成员

FStandardDelegateSignature MyStandardDelegate;
 


接着,我们新建一个 Actor 子类——DelegateListener,主要实现具体方法,以及负责委托的绑定和解绑

DelegateListener.h


  
  1. UCLASS()
  2. class TEST_API ADelegateListener : public AActor
  3. {
  4. GENERATED_BODY()
  5. public:
  6. // Sets default values for this actor's properties
  7. ADelegateListener();
  8. // Called when the game starts or when spawned
  9. virtual void BeginPlay() override;
  10. // Called every frame
  11. virtual void Tick( float DeltaSeconds ) override;
  12. UFUNCTION()
  13. void EnableLight();
  14. UFUNCTION()
  15. virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
  16. UPROPERTY()
  17. UPointLightComponent* PointLight;
  18. };


DelegateListener.cpp


  
  1. #include "Test.h"
  2. #include "UE4TestGameMode.h" // 注意 include 的位置
  3. #include "DelegateListener.h"
  4. // Sets default values
  5. ADelegateListener::ADelegateListener()
  6. {
  7. // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
  8. PrimaryActorTick.bCanEverTick = true;
  9. PointLight = CreateDefaultSubobject<UPointLightComponent>("PointLight");
  10. RootComponent = PointLight;
  11. PointLight->SetVisibility(false);
  12. }
  13. // Called when the game starts or when spawned
  14. void ADelegateListener::BeginPlay()
  15. {
  16. Super::BeginPlay();
  17. UWorld* TheWorld = GetWorld();
  18. if (TheWorld != nullptr)
  19. {
  20. AGameMode* GameMode = Cast<AGameMode>(UGameplayStatics::GetGameMode(TheWorld));
  21. AUE4TestGameMode * MyGameMode = Cast<AUE4TestkGameMode>(GameMode);
  22. if (MyGameMode != nullptr)
  23. {
  24. // ❤ 绑定一个基于 UObject 的成员函数的委托。UObject 委托保留了一个弱引用在你的对象上,可以通过.ExecuteIfBound() 来调用委托的函数
  25. MyGameMode->MyStandardDelegate.BindUObject(this,
  26. &ADelegateListener::EnableLight);// 其实就是绑定了 EnableLight 函数指针。
  27. }
  28. }
  29. }
  30. // Called every frame
  31. void ADelegateListener::Tick( float DeltaTime )
  32. {
  33. Super::Tick( DeltaTime );
  34. }
  35. void ADelegateListener::EnableLight()
  36. {
  37. PointLight->SetVisibility(true);
  38. }
  39. void ADelegateListener::EndPlay(const EEndPlayReason::Type EndPlayReason)
  40. {
  41. Super::EndPlay(EndPlayReason);
  42. UWorld* TheWorld = GetWorld();
  43. if (TheWorld != nullptr)
  44. {
  45. AGameMode* GameMode = Cast<AGameMode>(UGameplayStatics::GetGameMode(TheWorld));
  46. AUE4TestGameMode * MyGameMode = Cast<AUE4TestGameMode>(GameMode);
  47. if (MyGameMode != nullptr)
  48. {
  49. // 解绑委托
  50. MyGameMode->MyStandardDelegate.Unbind();
  51. }
  52. }
  53. }

值得注意的是,如果我们绑定的是普通的 C++ 函数,那么就应该将 BindUObject 改为 BindRaw,如果是静态方法,那就改为 BindStatic。

最后,回到我们之前的 MyTriggerVolume.cpp, 利用 GameMode(我们之前声明委托和定义委托成员的地方) 执行委托,

在 NotifyActorBeginOverlap 方法中添加以下代码:


  
  1. UWorld* TheWorld = GetWorld();
  2. if (TheWorld != nullptr)
  3. {
  4. AGameMode* GameMode = Cast<AGameMode>(UGameplayStatics::GetGameMode(TheWorld));
  5. AUE4TestGameMode * MyGameMode = Cast<AUE4TestGameMode>(GameMode);
  6. // ❤ 执行委托的函数
  7. MyGameMode->MyStandardDelegate.ExecuteIfBound();
  8. }

(未完待续)
 

文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/63834770

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。