• U++学习笔记 ------ 多播委托


    多播委托

    1、可以绑定多个回调函数,所有绑定的回调函数都会执行,实质是维持了一个单播委托的数组没有返回值支持参数不支持反射以及序列化

    绑定多播委托
    Add:将函数委托添加 到该多播委托的调用列表中。
    AddStatic:添加原始C++指针全局函数委托
    AddRaw:添加原始C++指针委托。原始指针不使用任何类型的引用,因此如果从委托下面删除了对象,则调用此函数可能不安全。调用Execute()时请小心!
    AddSP:添加基于共享指针的(快速、非线程安全)成员函数委托。共享指针委托保留对对象的弱引用。
    AddUObject:添加基于UObject的成员函数委托。UObject委托保留对对象的弱引用。
    Remove:从该多播委托的调用列表中删除函数(性能为O(N))。请注意,委托的顺序可能不会被保留!
    RemoveAll:从该多播委托的调用列表中删除绑定到指定UserObject的所有函数。请注意,委托的顺序可能不会被保留!

    只有动态多播委托才能暴露给蓝图

    TestCharacter.h:

    1. // Copyright Epic Games, Inc. All Rights Reserved.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "GameFramework/Character.h"
    5. #include "TestCharacter.generated.h"
    6. UCLASS(config=Game)
    7. class ATestCharacter : public ACharacter
    8. {
    9. GENERATED_BODY()
    10. public:
    11. ATestCharacter();
    12. virtual void EventBegin();
    13. void output();
    14. void output1();
    15. };

    TestCharacter.cpp:

    1. // Copyright Epic Games, Inc. All Rights Reserved.
    2. #include "TestCharacter.h"
    3. #include "HeadMountedDisplayFunctionLibrary.h"
    4. #include "Camera/CameraComponent.h"
    5. #include "Components/CapsuleComponent.h"
    6. #include "Components/InputComponent.h"
    7. #include "GameFramework/CharacterMovementComponent.h"
    8. #include "GameFramework/Controller.h"
    9. #include "GameFramework/SpringArmComponent.h"
    10. //
    11. // ATestCharacter
    12. ATestCharacter::ATestCharacter()
    13. {
    14. // Set size for collision capsule
    15. GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
    16. // set our turn rates for input
    17. BaseTurnRate = 45.f;
    18. BaseLookUpRate = 45.f;
    19. // Don't rotate when the controller rotates. Let that just affect the camera.
    20. bUseControllerRotationPitch = false;
    21. bUseControllerRotationYaw = false;
    22. bUseControllerRotationRoll = false;
    23. // Configure character movement
    24. GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
    25. GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
    26. GetCharacterMovement()->JumpZVelocity = 600.f;
    27. GetCharacterMovement()->AirControl = 0.2f;
    28. // Create a camera boom (pulls in towards the player if there is a collision)
    29. CameraBoom = CreateDefaultSubobject(TEXT("CameraBoom"));
    30. CameraBoom->SetupAttachment(RootComponent);
    31. CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
    32. CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
    33. // Create a follow camera
    34. FollowCamera = CreateDefaultSubobject(TEXT("FollowCamera"));
    35. FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
    36. FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
    37. // Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
    38. // are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
    39. }
    40. void ATestCharacter::output() {
    41. GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Green, TEXT("Sccessful"));
    42. }
    43. void ATestCharacter::output1()
    44. {
    45. GEngine->AddOnScreenDebugMessage(-1, 10.f, FColor::Green, TEXT("Sccessful Again"));
    46. }

    Delegator.h:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "GameFramework/Actor.h"
    5. #include "TestCharacter.h"
    6. #include "Delegator.generated.h"
    7. DECLARE_MULTICAST_DELEGATE(DeleLoader);
    8. UCLASS()
    9. class TEST_API ADelegator : public AActor
    10. {
    11. GENERATED_BODY()
    12. public:
    13. // Sets default values for this actor's properties
    14. ADelegator();
    15. DeleLoader Binding();
    16. private:
    17. DeleLoader Dele;
    18. class ATestCharacter* Testor = nullptr;
    19. protected:
    20. // Called when the game starts or when spawned
    21. virtual void BeginPlay() override;
    22. public:
    23. // Called every frame
    24. virtual void Tick(float DeltaTime) override;
    25. };

    Delegator.cpp:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "Delegator.h"
    3. #include "TestCharacter.h"
    4. // Sets default values
    5. ADelegator::ADelegator()
    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. }
    10. DeleLoader ADelegator::Binding()
    11. {
    12. if (Testor == nullptr) Testor = NewObject();
    13. Dele.AddUObject(Testor, &ATestCharacter::output);
    14. Dele.AddUObject(Testor, &ATestCharacter::output1);
    15. return Dele;
    16. }
    17. // Called when the game starts or when spawned
    18. void ADelegator::BeginPlay()
    19. {
    20. Super::BeginPlay();
    21. }
    22. // Called every frame
    23. void ADelegator::Tick(float DeltaTime)
    24. {
    25. Super::Tick(DeltaTime);
    26. }

    .DelegateInvoker.h:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #pragma once
    3. #include "CoreMinimal.h"
    4. #include "GameFramework/Actor.h"
    5. #include "Delegator.h"
    6. #include "DelegateInvokor.generated.h"
    7. UCLASS()
    8. class TEST_API ADelegateInvokor : public AActor
    9. {
    10. GENERATED_BODY()
    11. public:
    12. // Sets default values for this actor's properties
    13. ADelegateInvokor();
    14. UFUNCTION(BlueprintCallable)
    15. void Invoker();
    16. private:
    17. ADelegator* Dele = nullptr;
    18. class ATestCharacter* Testor = nullptr;
    19. protected:
    20. // Called when the game starts or when spawned
    21. virtual void BeginPlay() override;
    22. public:
    23. // Called every frame
    24. virtual void Tick(float DeltaTime) override;
    25. };

    DelegateInvoker.cpp:

    1. // Fill out your copyright notice in the Description page of Project Settings.
    2. #include "DelegateInvokor.h"
    3. #include "Delegator.h"
    4. // Sets default values
    5. ADelegateInvokor::ADelegateInvokor()
    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. }
    10. void ADelegateInvokor::Invoker()
    11. {
    12. if (Dele == nullptr) Dele = NewObject();
    13. Dele->Binding().Broadcast();
    14. }
    15. // Called when the game starts or when spawned
    16. void ADelegateInvokor::BeginPlay()
    17. {
    18. Super::BeginPlay();
    19. }
    20. // Called every frame
    21. void ADelegateInvokor::Tick(float DeltaTime)
    22. {
    23. Super::Tick(DeltaTime);
    24. }

    效果图如下:

    总结:可以对多个函数进行绑定执行


    学习参考:UE4中的委托及实现原理 - 知乎 (zhihu.com)

    事件委托:UE4 C++ 事件_declare_event_刹那天地宽的博客-CSDN博客


    1. DECLARE_EVENT(AMyTriggerVolume, FPlayerEntered)
    2. // 实例化一个事件
    3. FPlayerEntered OnPlayerEntered;
    4. //
    5. UFUNCTION()
    6. void OnTriggerEvent();
    7. // 绑定函数到事件
    8. OnPlayerEntered.AddUObject(this, &ATriggerVolEventListener::OnTriggerEvent);
    9. // 通过调用 Broadcast()使用此事件
    10. OnPlayerEntered.Broadcast();

    版权声明:本文为CSDN博主「netyeaxi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/netyeaxi/article/details/81673691 

  • 相关阅读:
    基于Matlab的遥感图像变化监测系统
    风力发电资质怎么办理,风力发电设计资质乙级办理条件
    第1讲 Android Camera Native Framework 课程介绍
    堆料,新能源汽车走上智能手机老路
    npm版本升级报错
    Python实现的公众号系统介绍及其中间件分析
    在Linux手机上编译和运行2048游戏学习C++
    TCP 协议有哪些缺陷?
    不同MySQL服务的表以及库的数据迁移(/备份)
    MySQL常见面试题(四)
  • 原文地址:https://blog.csdn.net/qqQQqsadfj/article/details/127782784