Create and Attach Custom Actor Component

I believe I have solved the problem. There are things to mention before the solution:

“AttachTo” method is belong to "USceneComponent"s. Therefore, you are not supposed to attach an “ActorComponent” to them. The idea of attaching a component which is not a “USceneComponent” is also not true. If your component does not have any attribute in 3d world, you are not supposed to attach it to the components having attributes in 3d world.

Solution:

  1. It is correct to create a component by using "CreateDefaultSubobject(TEXT("YourComponentName")).

  2. If you dive into the API document for [AActor][1], you find a method [AddOwnedComponent( UActorComponent * Component)][2]. Yes, this is the method you should call if you want to add an actor component into your actor.

Here is my SpellCaster component inside a blueprint of my “Hero” class:

11 Likes