FVector and pointers

Hi! I am coming from C#, so I want to double-check, that I get the basics here.

Here is my example code of getting players position:

// Called when the game starts
void UApplyHairVelocity::BeginPlay()
	myActor = this->GetOwner();
	lastFramePosition = myActor->GetActorLocation();

I defined myActor as AActor *, and lastFramePosition as FVector;

First one is the pointer, other is not. My logic is, that for FVector, I create new value when I run GetActorLocation() function to modify it later, but with getOwne(), I simply point to some actor class. Am I correct, or FVector needs to be FVector* too?

You are correct.

FVector should be a pointer too…

If you copy the vector object, you’ll need to pass it back latter; unless you have very good reasons for that, you should just create a reference to it, if not possible for what you need, then you make a pointer.

If you just reference the object, there’s close to no impact in code.
If you really need a pointer, each time you assign it you create another memory address for the same object.
If you copy the object, you better know what you’re doing otherwise you shouldn’t do that :stuck_out_tongue:

For many times you’ll need a new pointer, but keep in mind that you can also reference pointers using “&" to avoid creating too much garbage in memory.
You also shouldn’t forget to set ‘myPointer = nullptr’ when you don’t need it anymore.
Pointers are way way waayy less impact then a copy of the object, but they’re still a allocation in memory.
If you pass things as copy, without the "
” or “&” you’ll end up with a very buggy and heavy to run code later on…

That is what I’ve been learning so far, I also came from C#/python, if I’m wrong the C++ gods shaw correct me :wink:

This is… not true :stuck_out_tongue:

It’s totally fine to return things by value provided that you don’t want to modify the original, and that the object being copied isn’t particularly large or complex (and sometimes that’s okay too, but you have to be more careful).

Returning a pointer to a private, internal member of a class is a bad idea, generally - they’re private for a reason. In this case, however, making a pointer to the result of GetActorLocation doesn’t make sense, since that FVector returned is itself a copy of what’s inside the actor! You’d be pointing to a variable created inside the UApplyHairVelocity::BeginPlay function! You’re not avoiding any copies, just making things more complicated.

If the GetActorLocation returned an FVector*, then you could have FVector myPtrToAVector = myActor->GetActorLocation()* but that isn’t the case in the above example.

Lastly, don’t think that returning by pointer or reference will always be super helpful performance-wise - trust that the compiler can and will optimize a lot of code. Initializing a variable with the returned result of a function is really fast, for example, due to Return Value Optimization.

The above code would also fail to run if he made it a pointer as the object he was pointing to would go out of scope and cause an access violation on dereference somewhere else in the class.

Don’t overthink things too much in c++, keep it simple. As Hate said the compiler can easily optimize many simple things.

Also Ryu I hope you’re pumped for the new Street Fighter coming out for PC and PS :smiley:

It’s on topic cause they use the Unreal Engine…