Get actor from component in C++?

How can I get my parent actor from a component in C++? Currently, I give the parent to the child when I add the child but there might be a better solution.

3 Likes

Thx! I think I was just looking for GetOwner().

GetOwner()->GetRootComponent()->AttachParent->GetOwner()

  • GetOwner() → Get the actor that own the component
  • GetRootComponent() → Get the USceneComponent* of the actor
  • AttachParent → Get the parent, but it’s a USceneComponent*
  • GetOwner() → Get the owner of the parent
3 Likes

Is it allowed get a return AActor after doing Super::GetOwner()
Is it only allowed for the owner actor itself to call it directly as such?

Not sure to understand your question, but if your question is :
“Is it allowed to call Super::GetOwner() instead of just GetOwner() ?”

  • Yes, if you do not override GetOwner(), but you don’t have to.

“Is it only allowed for the owner actor to call GetOwner() ?”

  • You have to be careful because there is several GetOwner() functions. There is one on AActor, that may return you who spawned this Actor (“Owner of this Actor, used primarily for replication (bNetUseOwnerRelevancy & bOnlyRelevantToOwner) and visibility (PrimitiveComponent bOwnerNoSee and bOnlyOwnerSee)”), and another on UActorComponents, that will return you the actor that own the components.
    All those functions are “public” so they can be called anywhere.
1 Like