Download

Exposing UObject to both be extended and assigned to blueprint

How can I expose a UObject to both be extended by a blueprint and allow that blueprint to be assigned to a variable of another blueprint?

To better illustrate, let’s say I have an object to both have some logic and store animations:



UCLASS(Blueprintable, BlueprintType)
class MYGAME_API UAnimationHandler : public UObject
{
    GENERATED_BODY()

public:
    bool RunAnimation(const EAnimation Animation);

private:
    // ... Some pointers to animations
};


And I have an actor that holds a reference to it:



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

private:
    UPROPERTY(Category = "Animation", EditAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
    class UAnimationHandler* AnimationHandler;
};


Then I create a blueprint for the handler populating the animations.
And now I want to assign the handler blueprint to a blueprint of AMyActor.

When opening the box to assign AnimationHandler there are zero options and trying to drag and drop did not help.
Is there a way to make this work? Is there some configuration missing?



private:
    UPROPERTY(Category = "Animation", BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
    class UAnimationHandler* AnimationHandler;

public:
    UPROPERTY(Category = "Animation", EditAnywhere, BlueprintReadWrite)
    TSubclassOf<UAnimationHandler> AnimationClass;




(...)
{
    if (AnimationClass.Get())
    {
        AnimationHandler = NewObject<UAnimationHandler>(this,AnimationClass.Get(),*AnimationClass.Get()->GetName(),RF_Public,AnimationClass.Get()->GetDefaultObject(false));
    }
}


While the above method is perfectly functional, another (I think better) solution is to derive UAnimationHandler from UDataAsset instead of UObject (assuming you are only overriding data, not scripts, in the Editor). This method has two added benefits:

  1. The pointer is automatically instantiated at runtime, so no initialization boilerplate necessary
  2. You only need to maintain one UPROPERTY()

Thanks for your replies!

In the end I went with Bruno’s suggestion but next time I will try yours Mowgl as I agree it looks better.