What's the general use of a pointer? When should it be used?

Hi,

I get that pointers can save memory when referring to a variable but I don’t understand why we store the pointer of Owner via GetOwner() and then essentially save Owner.GetActorLocation() to a FVector.

Wouldn’t you just use FVector ActorLocation = GetActorLocation() directly? Or am I missing something and we are saving memory somehow?

As someone who has done a bit of Java in the past, I’m trying to wrap my head around the concept of a pointer and when it should be used.

You don’t have to store it in an intermediate variable. Some people just find it easier to read. The compiler can optimise trivial situations such as these.

We’re coding on a component. You would first need to get the actor it is attached to.

FVector ActorLocation = GetOwner()->GetActorLocation();

C++ uses value semantics whereas Java uses reference semantics.

void SomeFunction(T Value);
// calling it
SomeFunc(SomeObject);

Value is a copy of SomeObject for all T. If you want to refer to something that already exists you need to use pointers/references; for example, the actor that the component is attached to.

Ahhh, this makes more sense thanks.

I forgot that we are coding on the mover component.

So the GetOwner() function returns the reference, not the copy of the Actor the component is on?

Well a pointer, but yes. You can see that in the return type of the function

AActor* GetOwner() const

- UActorComponent::GetOwner | Unreal Engine 5.2 Documentation

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms