Using Transform Position

FString ObjectName = GetOwner()->GetName();
FVector ObjectPosition;
GetOwner()->GetTransform().TransformPosition(ObjectPosition);

UE_LOG(LogTemp, Warning, TEXT("Position of %s is %s"), *ObjectName,*ObjectPosition.ToString());

So when I say FVector ObjectPosition, I am not exactly allocation memory? or Am i? Because if I use new It has to be a pointer of some sort.

Also noticed that I pass the ObjectPosition directly into TransformPosition (though the parameter accepts a reference (&)). Please elaborate on this.

  • When you say FVector ObjectPosition;, you are telling the compiler that it will probably need to reserve a little memory for that variable in a region of memory called “the stack”, only while it executes the code in the scope where the variable lives. But you are not dynamically allocating memory, which happens when you use the new operator. Local variables are faster to work with and their lifetimes are automatically managed by the compiler, you don’t control exactly when they are allocated or deallocated. In your case, the “ObjectPosition” variable will probably be optimized away by the compiler and use no memory at all.

  • In c++, pointers and references are different. If the parameter of the function was a pointer, you would need to pass in a pointer or take the address of a variable with the & operator. But when the parameter is a reference, you can pass in a reference or the variable itself, and the function will get a reference to it automatically (instead of having to create a copy of its value, which would be needed if the parameter was not a reference).

Privacy & Terms