Different; works; but is it OK? - GetActorLocation().ToString()

What I used:

TransformPositionCPP

It’s shorter, but is there any reason not to use it?
Here’s the game output:

GameLog

Edit: Looking at other posts, I see that Transform gives Scale and Rotation as well, which may be important later. However, this works well for just Position.

1 Like

I came up with the same solution, I am a competent Java Developer so am used to trolling through intellisense / documentation, While the Section is called Transform, the field we wanted was Location so I used intellisense and typed ->Get then started typing Location and guess what - it found GetActorLocation()

Champion!!!

I came up with the same solution.So, wanted to know, is it correct?

Does it work? Then it’s correct. In either case, you are getting the x,y,z of the transform. I found 3 main ways:

FString pos1 = GetOwner()->GetTransform().GetLocation().ToString();
FString pos2 = GetOwner()->GetActorLocation().ToString();
FString pos3 = GetOwner()->GetTransform().GetTranslation().ToString();

There is a subtle difference between GetActorLocation() and GetTransform().SomethingElse. Mainly, if you are only dealing with the location and don’t need the other elements that are on the transform, it is a little less typing and less referencing. GetTransform() gets you the entire component and everything that comes with it, while GetActorLocation is a shortcut to just the location you might be looking for.

I doubt there is much performance difference between these because of the compiler (if it were an interpreted language, GetActorLocation is calling less functions which would be faster).

The thing I’m not sure of is why there is both GetLocation() and GetTranslation() that seem to return the same values. The unreal docs are pretty terrible on this. GetTranslation “Returns the translation component”, while GetLocation gives you a “Temp function for easy conversion”.

Easy? I’m not learning C++ for easy! GetTranslation it is.

Yup this is definitely okay. What happens here is, the GetTransform() holds the mesh’s Location, Rotation and Scale, all three and we only want the location for this part. So GetActorLocation() just gets the location out of the mesh, which is what we want.

This is what my code looks like :
Please ignore the background image and the weird colors

1 Like

Hello how did you get to implement ObjectLocation.x,y,z. ?
I mean, as you found out, was it just a coincidence while you were experimenting, or did you know that, could you please tell me?

Hello how did you get to implement ObjectLocation.x,y,z. ?
I mean, as you found out, was it just a coincidence while you were experimenting, or did you know that, could you please tell me?

You can only know before hand if you’ve done it before otherwise you search to find out (be it online or through code in one way or another - Adding this as a blanket for all such as experimentation which requires searching or learning through this course, etc).

Normally, nearly all things you run into have already been answered online so searching there is typically the fastest. The only time this doesn’t happen normally is if its new and no one has yet to ask or you manage to do something no one already thought of (but if its wrong then you may not find an answer either).

On the topic of the OP, its fine to use anything that has the correct end result however, there are times when that is incorrect. It could be that one way is expressly supported and the others could stop working at any time. At the same time, it could be that all become deprecated in the future and oh joy how fun lol. If there’s no specific use that Epic states should be used then its whatever works and whatever happens is what happens.

Experimenting to be honest. I found out that an FVector is a struct with three floats - X,Y,Z (and more ofcourse). And so I just used them.

struct FVector 
{
public:

	/** Vector's X component. */
	float X;

	/** Vector's Y component. */
	float Y;

	/** Vector's Z component. */
	float Z;

Thats a small copy paste of the FVector. I pressed Control + Left Click on the name ‘FVector’ to get there. Really good if you wanna access its codings.

It works so long as all you need is Location.