Getting Transforms in C++


#1

I achieved the same results as was shown in the video, but did something slightly different. This is my line of code:
FString ObjectPos = GetOwner()->GetActorLocation().ToString();

As you can see the outcome is the same. My question would be, is there simply multiple ways of achieving the same result, or is there a preferred method that results in fewer side effects (if there could be any)?


#2

There are different reasons people would want to do different ways, but yes, you’re right that there are just a thousand ways to skin a cat.

Look at it like this, if you’re doing this every tick:

FString ChairPosition = GetOwner()->GetActorLocation().ToString();
UE_LOG(LogTemp, Warning, TEXT("Chair is at: %s"), *ChairPosition);

Then every cycle, your game is taking the time to declare a variable, run the function, print the statement and pull the value from memory…

Whereas if you’re doing this:
UE_LOG(LogTemp, Warning, TEXT("Chair is at: %s"), *GetOwner()->GetActorLocation().ToString());

Every cycle, you’re printing the statement and running the function, without the extra reserving and calling from memory.

Now, to be fair, you’re saving tiny fractions of a second each time you do this. Milliseconds, probably. But, the larger the datatype, I would have to assume the more you’re saving. The more methods going on in your scene, the more impact those choices would have, I would have to imagine.

HOWEVER… The downside to that is, you’re going to have to get used to seeing more “cryptic” code, which is a bit of a hassle for newer people, and makes coming back after time away a bit more difficult. It’s a bit easier to understand what a variable called “ChairPosition” is after 3 months away than calling a method on a returned value from a function call from a dereferenced pointer value. lol :stuck_out_tongue:


#3

If you want the transform get the transform, if you want the location but nothing else that’s in the transform. Just get the location, don’t see why you would spend the extra time typing that if you only wanted the location.


#4

The thing here is that you’re relying on the ToString function to get the data into a string format. Epic’s Documentation shows this. I did this as well at first.

Doing this means you’re gonna side step the fact you’re geting a FVector from GetActorLocation().
You’ll see that you can get the X,Y,Z components of the vector. So I rewrote it so my message looks like

TEXT("%s is at (%f,%f,%f)"),
Where %s is the object name and the 3 %f’s are the XYZ part so the vector.
So I have “BlueCube is at (-615.000000,453.500000,356.500000)”