Download

Retrieving Parent Actor Location from ActorComponent, Beginner Question

Hello,

I’m somewhat new to UE4 and could use some help to better understand some functionality.
For context, I’m trying to create a component that simply spawns a mesh next to its parent actor. I would also like it to follow the actor but maintain it’s own relative position.

What brings me here is that I can’t quite figure out how to grab the relative position of the parent actor. What seems to be happening is that I’m grabbing some default or uninitialized location (so (0.0f, 0.0f, 0.0f). When I drag my pawn into the scene and release, my component mesh snaps to that centered location. This is shown below, where the gold sphere is my ActorComponent:

Here’s the code snippet from the constructor of my ActorComponent:



// OrbitActorComponent.cpp
...

// Get Parent Actor
AActor* ParentActor = GetOwner();

if (ParentActor)
{
      UE_LOG(LogTemp, Warning, TEXT("The owner is valid!"));
}

// Get Parent Location
FVector ParentLocation;

if (ParentActor)
{
     UE_LOG(LogTemp, Warning, TEXT("We have a location!"));
     ParentLocation = ParentActor->GetActorLocation();
}

// Specify mesh for the rotating object
OurOrbitMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OrbitRepresentation"));

static ConstructorHelpers::FObjectFinder<UStaticMesh> SphereVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Sphere.Shape_Sphere"));
static ConstructorHelpers::FObjectFinder<UMaterial> SphereMaterial(TEXT("/Game/StarterContent/Materials/M_Metal_Gold.M_Metal_Gold"));

if (SphereVisualAsset.Succeeded())
{
     OurOrbitMesh->SetStaticMesh(SphereVisualAsset.Object);
     OurOrbitMesh->SetWorldLocation(FVector(ParentLocation.X + 10.f, ParentLocation.Y, ParentLocation.Z));
     OurOrbitMesh->SetWorldScale3D(FVector(0.8f));
}

...


So GetActorLocation() is returning (0.0f, 0.0f, 0.0f) rather than (220.0f, 350.0f, 150.0f) which is where the pawn is located in the image above?

In my actor header I create an instance of the actor component:



// CollidingPawn.h
...

// Orbiting Component
UPROPERTY()
      class UOrbitActorComponent* OurOrbitComponent;
...


And specify the subobject:



// CollidingPawn.cpp
...

// Instance of orbit component
OurOrbitComponent = CreateDefaultSubobject<UOrbitActorComponent>(TEXT("OrbitComponent"));
...


Additionally, when I click Play the component mesh actually moves with my pawn and maintains a fixed distance? If someone could explain that behaviour to me it would be incredibly helpful. Is it inheriting the input functionality from my pawn?

Thank you!

You are trying to SetWorldLocation of a StaticMeshComponent wich is a child of an ActorComponent wich is a child of your Pawn. This is looking for trouble.
I think a better route would be to simply SetRelativeLocationof OurOrbitComponent which should be (if you wanna it to be 10cm front of the Pawn) FVector(10,0,0).

Hi silik1, thank you for the reply.

I see what you’re saying, I’m coupling it deeper than it needs to be. I don’t think OurOrbitComponent actually inherits SetRelativeLocation() from ActorComponent? Because this is a custom actor component, do I need to define this functionality myself?

I know you’re saying to save myself trouble by specifying the location of the actor component in the pawn. Now, if I wanted to add this orbiting actor component to multiple pawns, would it not make sense to define the location inside the actor component itself? I’m just thinking of it in terms of modularity.

Let me know if I’m being unclear, thanks!

You are right, UActorComponent doesn’t inherits SetRelativeLocation… My bad, Sorry. I often mix up ActorComponent and an Attached Actor…

May be you should do just that? Make an AActor OurOrbitActor, attach it to the Pawn, that would be easy to manipulate with SetRelativeLocation()…

Sorry again to have mislead you :slight_smile: