1.C++原生代理

单播代理 绑定单个函数
多播代理 绑定多个函数 没有返回值

目的:调用函数

特性:1. 绑定 和 调用 是分开的,但其实放在哪个类里面需要视情况而定
2. 可使用代理动态绑定到任意对象的成员函数

如果不使用代理,对象A调用对象B的函数,对象A中必须有指向B对象的指针,通过指针来调用函数;
如果使用代理,对象A和对象B之间会有一个Delegate,这个代理定义好之后,在对象A中声明代理对象,绑定函数,然后就可以调用了,绑定也可以不在对象A中进行,这样对象A就完全不知道调用的是谁的函数了

//原生C++的绑定
bindRaw

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include <Engine/Engine.h>
  5. static void SrcPrint(FString newstring)
  6. {
  7. if (GEngine)
  8. {
  9. GEngine->AddOnScreenDebugMessage(-1, 20, FColor::Red, newstring);
  10. }
  11. }
  12. DECLARE_DELEGATE(FDelegateTestA);
  13. DECLARE_DELEGATE_OneParam(FDelegateTestB,FString&);
  14. DECLARE_DELEGATE_RetVal(bool,FDelegateTestC);
  15. DECLARE_DELEGATE_RetVal_OneParam(bool,FDelegateTestD,FString&);
  16. //调用者
  17. class classA
  18. {
  19. public:
  20. FDelegateTestA DelegateTestA;
  21. FDelegateTestB DelegateTestB;
  22. FDelegateTestC DelegateTestC;
  23. FDelegateTestD DelegateTestD;
  24. //调用
  25. void init()
  26. {
  27. bool IsRight = false;
  28. FString NewString = TEXT("Hello World!");
  29. DelegateTestA.ExecuteIfBound();
  30. DelegateTestB.ExecuteIfBound(NewString);
  31. if (DelegateTestC.IsBound())
  32. {
  33. IsRight = DelegateTestC.Execute();
  34. }
  35. if (DelegateTestD.IsBound())
  36. {
  37. IsRight = DelegateTestD.Execute(NewString);
  38. }
  39. }
  40. };
  41. //绑定者 其实可以考虑把函数写在别的类中
  42. class ClassB
  43. {
  44. public:
  45. ClassB(classA* m_classA)
  46. {
  47. if(m_classA)
  48. {
  49. classa = m_classA;
  50. classa->DelegateTestA.BindRaw(this,&ClassB::ExecuteA); //原生C++的绑定
  51. classa->DelegateTestB.BindRaw(this, &ClassB::ExecuteB);
  52. classa->DelegateTestC.BindRaw(this,&ClassB::ExecuteC);
  53. classa->DelegateTestD.BindRaw(this,&ClassB::ExecuteD);
  54. }
  55. }
  56. void ExecuteA()
  57. {
  58. SrcPrint(TEXT("ExecuteA"));
  59. }
  60. void ExecuteB(FString& mystring)
  61. {
  62. SrcPrint(TEXT("ExecuteB"+mystring));
  63. }
  64. bool ExecuteC()
  65. {
  66. SrcPrint(TEXT("ExecuteC , bool is false"));
  67. return false;
  68. }
  69. bool ExecuteD(FString& mystring)
  70. {
  71. SrcPrint(TEXT("ExecuteD , bool is true"));
  72. return true;
  73. }
  74. ~ClassB()
  75. {
  76. if (classa)
  77. {
  78. classa->DelegateTestA.Unbind();
  79. classa->DelegateTestB.Unbind();
  80. classa->DelegateTestC.Unbind();
  81. classa->DelegateTestD.Unbind();
  82. classa->init();
  83. delete classa;
  84. classa = nullptr;
  85. }
  86. }
  87. private:
  88. classA* classa;
  89. };
  1. void ADelegate_Call::BeginPlay()
  2. {
  3. Super::BeginPlay();
  4. classA* newclassA = new classA();
  5. ClassB* newclassB = new ClassB(newclassA);
  6. newclassA->init();
  7. delete newclassB;
  8. newclassA = nullptr;
  9. newclassB = nullptr;
  10. //
  11. }

2.用共享指针绑定

存疑,人宅先做了一个把指针转成引用的函数,但并没有调用,好像指针就可以直接绑定成功
bindSP

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include <Engine/Engine.h>
  5. static void SrcPrint(FString newstring)
  6. {
  7. if (GEngine)
  8. {
  9. GEngine->AddOnScreenDebugMessage(-1, 20, FColor::Red, newstring);
  10. }
  11. }
  12. DECLARE_DELEGATE(FDelegateTestA);
  13. DECLARE_DELEGATE_OneParam(FDelegateTestB,FString&);
  14. DECLARE_DELEGATE_RetVal(bool,FDelegateTestC);
  15. DECLARE_DELEGATE_RetVal_OneParam(bool,FDelegateTestD,FString&);
  16. //调用者
  17. class classA
  18. {
  19. public:
  20. FDelegateTestA DelegateTestA;
  21. FDelegateTestB DelegateTestB;
  22. FDelegateTestC DelegateTestC;
  23. FDelegateTestD DelegateTestD;
  24. //调用
  25. void init()
  26. {
  27. bool IsRight = false;
  28. FString NewString = TEXT("Hello World!");
  29. DelegateTestA.ExecuteIfBound();
  30. DelegateTestB.ExecuteIfBound(NewString);
  31. if (DelegateTestC.IsBound())
  32. {
  33. IsRight = DelegateTestC.Execute();
  34. }
  35. if (DelegateTestD.IsBound())
  36. {
  37. IsRight = DelegateTestD.Execute(NewString);
  38. }
  39. }
  40. };
  41. //绑定者 其实可以考虑把函数写在别的类中
  42. class ClassB :public TSharedFromThis<ClassB>
  43. {
  44. public:
  45. void init()
  46. {
  47. classa = MakeShareable(new classA());
  48. classa->DelegateTestA.BindSP(this,&ClassB::ExecuteA); //原生C++的绑定
  49. classa->DelegateTestB.BindSP(this, &ClassB::ExecuteB);
  50. classa->DelegateTestC.BindSP(this,&ClassB::ExecuteC);
  51. classa->DelegateTestD.BindSP(this,&ClassB::ExecuteD);
  52. classa->init();
  53. }
  54. void ExecuteA()
  55. {
  56. SrcPrint(TEXT("ExecuteA"));
  57. }
  58. void ExecuteB(FString& mystring)
  59. {
  60. SrcPrint(TEXT("ExecuteB"+mystring));
  61. }
  62. bool ExecuteC()
  63. {
  64. SrcPrint(TEXT("ExecuteC , bool is false"));
  65. return false;
  66. }
  67. bool ExecuteD(FString& mystring)
  68. {
  69. SrcPrint(TEXT("ExecuteD , bool is true"));
  70. return true;
  71. }
  72. ~ClassB()
  73. {
  74. if (classa.Get())
  75. {
  76. classa->DelegateTestA.Unbind();
  77. classa->DelegateTestB.Unbind();
  78. classa->DelegateTestC.Unbind();
  79. classa->DelegateTestD.Unbind();
  80. classa->init();
  81. classa = nullptr;
  82. }
  83. }
  84. //FORCEINLINE TSharedRef<classA> get_classa(){return classa.ToSharedRef();}
  85. private:
  86. TSharedPtr<classA> classa;
  87. };
  1. void ADelegate_Call::BeginPlay()
  2. {
  3. Super::BeginPlay();
  4. TSharedPtr<ClassB> m_classb = MakeShareable(new ClassB());
  5. m_classb->init();
  6. }

3.bindUobject

绑定的是UObject的成员函数

  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #pragma once
  3. #include "CoreMinimal.h"
  4. #include <Engine/Engine.h>
  5. static void SrcPrint(FString newstring)
  6. {
  7. if (GEngine)
  8. {
  9. GEngine->AddOnScreenDebugMessage(-1, 20, FColor::Red, newstring);
  10. }
  11. }
  12. DECLARE_DELEGATE(FDelegateTestA);
  13. DECLARE_DELEGATE_OneParam(FDelegateTestB,FString&);
  14. DECLARE_DELEGATE_RetVal(bool,FDelegateTestC);
  15. DECLARE_DELEGATE_RetVal_OneParam(bool,FDelegateTestD,FString&);
  16. //调用者
  17. class classA
  18. {
  19. public:
  20. FDelegateTestA DelegateTestA;
  21. FDelegateTestB DelegateTestB;
  22. FDelegateTestC DelegateTestC;
  23. FDelegateTestD DelegateTestD;
  24. //调用
  25. void init()
  26. {
  27. bool IsRight = false;
  28. FString NewString = TEXT("Hello World!");
  29. DelegateTestA.ExecuteIfBound();
  30. DelegateTestB.ExecuteIfBound(NewString);
  31. if (DelegateTestC.IsBound())
  32. {
  33. IsRight = DelegateTestC.Execute();
  34. }
  35. if (DelegateTestD.IsBound())
  36. {
  37. IsRight = DelegateTestD.Execute(NewString);
  38. }
  39. }
  40. };
  41. //绑定者 其实可以考虑把函数写在别的类中
  42. class ClassB :public TSharedFromThis<ClassB>
  43. {
  44. public:
  45. ClassB()
  46. {
  47. classa = MakeShareable(new classA());
  48. }
  49. void ExecuteA()
  50. {
  51. SrcPrint(TEXT("ExecuteA"));
  52. }
  53. void ExecuteB(FString& mystring)
  54. {
  55. SrcPrint(TEXT("ExecuteB"+mystring));
  56. }
  57. bool ExecuteC()
  58. {
  59. SrcPrint(TEXT("ExecuteC , bool is false"));
  60. return false;
  61. }
  62. bool ExecuteD(FString& mystring)
  63. {
  64. SrcPrint(TEXT("ExecuteD , bool is true"));
  65. return true;
  66. }
  67. ~ClassB()
  68. {
  69. if (classa.Get())
  70. {
  71. classa->DelegateTestA.Unbind();
  72. classa->DelegateTestB.Unbind();
  73. classa->DelegateTestC.Unbind();
  74. classa->DelegateTestD.Unbind();
  75. classa->init();
  76. classa = nullptr;
  77. }
  78. }
  79. FORCEINLINE TSharedRef<classA> get_classa(){return classa.ToSharedRef();}
  80. private:
  81. TSharedPtr<classA> classa;
  82. };
void ADelegate_Call::BeginPlay()
{
    Super::BeginPlay();


    ClassB* newclassB = new ClassB();
    newclassB->get_classa()->DelegateTestA.BindUObject(this, &ADelegate_Call::printff);
    newclassB->get_classa()->init();

}



void ADelegate_Call::printff()
{
    SrcPrint(TEXT("hahahha"));
}

4.bindStatic

void ADelegate_Call::BeginPlay()
{
    Super::BeginPlay();

    ClassB* newclassB = new ClassB();

    newclassB->get_classa()->DelegateTestA.BindStatic(SrcPrint2);
    newclassB->get_classa()->init();

}
static void SrcPrint2()
{
    SrcPrint(TEXT("wawawawa"));
}

5.bindUfunction

UCLASS()
class CPLUSSTUDY_API ADelegate_Call : public AActor
{
    GENERATED_BODY()

public:    
    // Sets default values for this actor's properties
    ADelegate_Call();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    UFUNCTION()//必须写
    void printff();

};
void ADelegate_Call::BeginPlay()
{
    Super::BeginPlay();


    ClassB* newclassB = new ClassB();

    newclassB->get_classa()->DelegateTestA.BindUFunction(this, TEXT("printff"));

    newclassB->get_classa()->init();

}

6.多播代理


DECLARE_MULTICAST_DELEGATE_OneParam(FDelegateTestF, FString&);

//调用者
class classA 
{
public:
    FDelegateTestA DelegateTestA;
    FDelegateTestB DelegateTestB;
    FDelegateTestC DelegateTestC;
    FDelegateTestD DelegateTestD;
    FDelegateTestF DelegateTestF;

    //调用
    void init()
    {
        bool IsRight = false;
        FString NewString = TEXT("Hello World!");
        FString newstr = TEXT("happy newyear");

        DelegateTestA.ExecuteIfBound();
        DelegateTestB.ExecuteIfBound(NewString);

        if (DelegateTestC.IsBound())
        {
            IsRight = DelegateTestC.Execute();
        }

        if (DelegateTestD.IsBound())
        {
            IsRight = DelegateTestD.Execute(NewString);
        }
        DelegateTestF.Broadcast(newstr);

    }
};



//绑定者  其实可以考虑把函数写在别的类中
class ClassB :public TSharedFromThis<ClassB>
{
public:
    void init()
    {
        classa = MakeShareable(new classA());

        classa->DelegateTestF.AddRaw(this, &ClassB::ExecuteB);
        classa->DelegateTestF.AddRaw(this, &ClassB::ExecuteD);
        classa->init();

    }

    ClassB()
    {
        //classa = MakeShareable(new classA());
    }

    void ExecuteA()
    {
        SrcPrint(TEXT("ExecuteA"));
    }

    void ExecuteB(FString& mystring)
    {
        SrcPrint(TEXT("ExecuteB"+mystring));
    }

    bool ExecuteC()
    {
        SrcPrint(TEXT("ExecuteC , bool is false"));
        return false;
    }

    void ExecuteD(FString& mystring)
    {
        SrcPrint(TEXT("ExecuteD , bool is true"));
        //return true;
    }

    ~ClassB()
    {
        if (classa.Get())
        {
            classa->DelegateTestA.Unbind(); 
            classa->DelegateTestB.Unbind();
            classa->DelegateTestC.Unbind();
            classa->DelegateTestD.Unbind();
            classa->DelegateTestF.Clear();

            classa->init();

            classa = nullptr;
        }
    }
    FORCEINLINE TSharedRef<classA> get_classa(){return classa.ToSharedRef();}
private:
    TSharedPtr<classA> classa;
};
void ADelegate_Call::BeginPlay()
{
    Super::BeginPlay();


    ClassB* newclassB = new ClassB();

    newclassB->init();


}

7.动态绑定代理

动态单播代理只能绑一个函数,再绑定会把之前的顶掉
动态多播代理可以绑定多个函数
动态绑定代理 支持序列化 (被绑定的函数所在的类应该继承与Uobject)

动态单播代理的调用和原生代理是一样的 executeifbound
动态多播代理的调用用broadcast

动态代理可以暴露给蓝图,支持uobject

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include <Engine/Engine.h>
#include "my_function.generated.h"

static void SrcPrint(FString newstring) 
{
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 20, FColor::Red, newstring);
    }
}


DECLARE_DYNAMIC_DELEGATE_OneParam(FDelegateTestG, FString&,Str_c);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDelegateTestK, FString&, Str_c);


UCLASS()
class  UTestObject :public UObject
{
    GENERATED_BODY()
public:
    UFUNCTION()
    void initit()
    {
        classA* newclassa = new classA();
        newclassa->DelegateTestG.BindDynamic(this, &UTestObject::ptf);
        newclassa->DelegateTestG.BindDynamic(this, &UTestObject::ptff);
        newclassa->DelegateTestK.AddDynamic(this, &UTestObject::ptfff);
        newclassa->DelegateTestK.AddDynamic(this, &UTestObject::ptf);
        newclassa->init();
        delete newclassa;
    }
    UFUNCTION()
    void ptf(FString& strword)
    {
        SrcPrint(TEXT("ptf") + strword);
    }
    UFUNCTION()
    void ptff(FString& strword)
    {
        SrcPrint(TEXT("ptff")+strword);
    }
    UFUNCTION()
    void ptfff(FString& strword)
    {
        SrcPrint(TEXT("ptfff") + strword);
    }

protected:
private:
};
void ADelegate_Call::BeginPlay()
{
    Super::BeginPlay();

    UTestObject* myobject = NewObject<UTestObject>(this);
    myobject->initit();

}

8.Event绑定代理(多态绑定)

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include <Engine/Engine.h>
#include "my_function.generated.h"

static void SrcPrint(FString newstring) 
{
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 20, FColor::Red, newstring);
    }
}



//类似用一个类管理很多事件代理? 可能有的已经想好怎么写,有的还没有想好,就只能继承给子类
class FEventTask
{
public:
    DECLARE_EVENT_OneParam(FEventTask,EventA,FString&);  //创建一个EventA类,把FEventTask设置为它的友类
    virtual EventA& OnEventTaskDeclareA() = 0;

    DECLARE_EVENT_OneParam(FEventTask, EventB, FString&);
    EventB& OnEventTaskDeclareB() 
    {
        return m_eventB;
    };
    virtual ~FEventTask() {};
protected:
private:
    EventB m_eventB;
};

//这个子类实现了 未实现的方法,并写了一个调用的方法  这一部分应该是封装的?
class FEventTask_M :public FEventTask
{
public:
    DECLARE_DERIVED_EVENT(FEventTask_M, FEventTask::EventA, EventC); //声明一个派生事件委托,该委托的工作方式与其父类型相同,但目的是供不同的所属类型使用
    virtual EventC& OnEventTaskDeclareA() { return m_eventC; }

    void init()
    {
        FString NNN = TEXT("GUOJIANPENG");
        OnEventTaskDeclareA().Broadcast(NNN);
        FString MMM = TEXT("DAHAOREN");
        OnEventTaskDeclareB().Broadcast(MMM);
    }

protected:
private:
    EventC m_eventC;
};

//绑定者  间接调用者
class BroadTask
{
public:
    void init()
    {
        FEventTask_M* mytask = new FEventTask_M();
        mytask->OnEventTaskDeclareA().AddRaw(this, &BroadTask::ExecuteB);
        mytask->OnEventTaskDeclareA().AddRaw(this, &BroadTask::ExecuteD);

        mytask->OnEventTaskDeclareB().AddRaw(this, &BroadTask::ExecuteB);
        mytask->OnEventTaskDeclareB().AddRaw(this, &BroadTask::ExecuteD);
        mytask->init();
        delete mytask;
    }
    void ExecuteA()
    {
        SrcPrint(TEXT("ExecuteA"));
    }

    void ExecuteB(FString& mystring)
    {
        SrcPrint(TEXT("ExecuteB" + mystring));
    }

    bool ExecuteC()
    {
        SrcPrint(TEXT("ExecuteC , bool is false"));
        return false;
    }

    void ExecuteD(FString& mystring)
    {
        SrcPrint(TEXT("ExecuteD , bool is true"));
        //return true;
    }
protected:
private:
};
void ADelegate_Call::BeginPlay()
{
    Super::BeginPlay();

    BroadTask* mytask = new BroadTask();
    mytask->init();
    delete mytask;

}