Delegate Broadcasted from UStruct and Subscribed from UClass

anil021
Wed, 01 Dec 2021 17:06:30 +0000
Editor Scripting

Hello, I have a ustruct to hold some attributes like health. Whenever a change occurs in this attribute, I wan’t to fire a delegate. Subscribed uobjects will act differently depending on their function. For a brief example this code is what I want to achieve.

Let’s say my struct file looks something like this :

DECLARE_MULTICAST_DELEGATE(FOnChangeEvent)

USTRUCT()
struct FMyAttribute
{
    GENERATED_BODY()

    FOnChangeEvent OnChangeEvent;

    float MyValue ;
    void SetFloat(const float NewValue)
    {
        MyValue = NewValue;
        OnChangeEvent.Broadcast();
    }
}

And let’s say I have a MyAttribue member in my uclass header and this is my uclass.cpp file :

void MyClass::BeginPlay()
{
    Super::BeginPlay();
    MyAttribute.OnChangeEvent.AddUObject(this, &MyClass::MySubscribeFunc);
}

void MyClass::MySubscribeFunc()
{
    UE_LOG(LogTemp, Warning, TEXT("Hey I Recieved!") );
}

So the problem is, MySubscribeFunc is not subscribing to delegate. I checked OnChangeEvent.IsBound() after calling AddUObject and it returns false.
If I change my struct to a uclass, everything works. So, Is there anyway to make it work with UStructs or must I always use UClass to broadcast a delegate whenever I want to recieve it from another UClass ?

1 post - 1 participant