0
\$\begingroup\$

I have multiple different classes which need to perform the same complex operation
So to keep my code dry, I'm using a manager object which requires a delegate for the complex operation

Here's a simplified example:

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnChange);

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

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

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

        ...
     }


   private:
    FOnChange* OnChange;
    UObject* Owner;
};

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

   public:
    UPROPERTY(BlueprintAssignable)
    FOnChange OnChangeA;

    UPROPERTY()
    UMyManager* Manager;

    virtual void BeginPlay() {
        Super::BeginPlay();
        Manager->Setup(this, OnChangeA);
    }
};

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

   public:
    UPROPERTY(BlueprintAssignable)
    FOnChange OnChangeB;

    UPROPERTY()
    UMyManager* Manager;

    virtual void BeginPlay() {
        Super::BeginPlay();
        Manager->Setup(this, OnChangeB);
    }
};

Will this cause any unexpected behaviour or problems? Or should I take a completely different approach which will allow me to use delegates in a similar way

Note: Please do not suggest using Blueprint Function Library, The manager also handles other functionality and has references elsewhere. I've only simplified it in the example

\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.