11.4 C
New York
Friday, October 31, 2025

c++ – Storing reference of delegate in pointer


I’ve a number of completely different courses which must carry out the identical advanced operation
So to maintain my code dry, I am utilizing a supervisor object which requires a delegate for the advanced operation

Here is a simplified instance:

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnChange);

UCLASS()
class MYPROJECT_API UMyManager : public UObject {
    GENERATED_BODY()

   public:
    UFUNCTION()
    void Setup(UObject* NewOwner, FOnChange& NewOnChange) {  
        Proprietor = NewOwner;
        OnChange = &NewOnChange;  
    }

    UFUNCTION()
    void DoComplex(){ 
        if(Proprietor == nullptr || OnChange == nullptr){
            return;
        }

        ...
     }


   non-public:
    FOnChange* OnChange;
    UObject* Proprietor;
};

UCLASS()
class MYPROJECT_API AMyActor : public AActor {
    GENERATED_BODY()

   public:
    UPROPERTY(BlueprintAssignable)
    FOnChange OnChangeA;

    UPROPERTY()
    UMyManager* Supervisor;

    digital void BeginPlay() {
        Tremendous::BeginPlay();
        Supervisor->Setup(this, OnChangeA);
    }
};

UCLASS()
class MYPROJECT_API AMyCharacter : public ACharacter {
    GENERATED_BODY()

   public:
    UPROPERTY(BlueprintAssignable)
    FOnChange OnChangeB;

    UPROPERTY()
    UMyManager* Supervisor;

    digital void BeginPlay() {
        Tremendous::BeginPlay();
        Supervisor->Setup(this, OnChangeB);
    }
};

Will this trigger any sudden behaviour or issues? Or ought to I take a very completely different method which can permit me to make use of delegates in an analogous approach

Observe:
Please don’t recommend utilizing Blueprint Perform Library, The supervisor additionally handles different performance and has references elsewhere. I’ve solely simplified it within the instance

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles