How to attach Custom Actor as Component of Pawn, c++

Hello,

Here is my scenario:

I have a pawn, MyPawn. MyPawn has a UPROPERTY MySpriteComponent, I have extended MyPawn with a blueprint and MySpriteComponent is editable in that context.

I also have a custom actor class MyCustomActor. I want to add it as another UPROPERTY to MyPawn of type MyCustomActor and attach it to MySpriteComponent.

My goal is to create several blueprints extending MySpriteComponent and to be able to set any of them as the MyCustomActor instance on MyPawn.

I would like the component list in the blueprint to look like:

  • RootComponent
    • MySpriteComponent
      • MyCustomActor

Where the MyCustomActor component allows me to assign some blueprint child of MyCustomActor.

I have tried many things, I want something like this to work:

(In the constructor of MyPawn):

MyCustomActor = CreateOptionalDefaultSubobject<AMyCustomActor >(AMyPawn::MyCustomActorName);
	if (MyCustomActor) {
		MyCustomActor->AttachToComponent(MySpriteComponent,FAttachmentTransformRules::KeepRelativeTransform);
}

This results in only RootComponent and MySpriteComponent showing up on the blueprint.

Any help would be appreciated!

Thanks,
Jeremy

I don’t think you can spawn an actor using CreateOptionalDefaultSubObject. Instantiating an Actor is more complicated than a UObject (it needs it’s own reference to the world, etc).

You could try using SpawnActor instead, but I’m not sure if it will work in the constructor.

To store a child BP of a given class, you can use TSubclassOf; or TAssetSubclassOf if you want to stream the class data.

.h:

 UPROPERTY()
TSubclassOf<MyCustomActor> MyBPClass;

UPROPERTY()
MyCustomActor* MyAttachedCustomActor

.cpp:

...constructor stuff...

UWorld* MyWorld = GetWorld();

if (MyWorld) {
  MyAttachedCustomActor = MyWorld::SpawnActor<MyCustomActor>(MyBPClass);

}

if (MyAttachedCustomActor) {
  MyCustomActor->AttachToComponent(MySpriteComponent,FAttachmentTransformRules::KeepRelativeTransform);

... etc. do other setup actions...

}

“My goal is to create several blueprints extending MySpriteComponent and to be able to set any of them as the MyCustomActor instance on MyPawn.”

You won’t be able to create MyCustomActor instances of MySpriteComponent, unless MySpriteComponent is an Actor - but then it wouldn’t be a Component (which is an UObject). If you meant you want to extend MyCustomActor using blueprints, then you should be fine and the above should work, assuming you can spawn an actor in the constructor.

If not, you’ll have to spawn them dynamically outside the constructor, or outside of MyPawn entirely.

What I ended up doing is attaching a UChildActorComponent.

UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "MyCustomActor", meta = (AllowPrivateAccess = "true"))
		class UChildActorComponent* MyCustomActor;

Then in the cpp:

MyCustomActor= CreateOptionalDefaultSubobject<UChildActorComponent>(TEXT("MyCustomActorName"));
	MyCustomActor->SetupAttachment(MySpriteComponent);

This ended up giving me the functionality I was looking for.