Building Escape - Getting an Actors Transform

Hello All,

I was going through this lecture saying whiskey, tango, foxtrot a lot. I couldn’t figure out what the lecturer wanted when he basically said get the transform of whatever “actor” using FString I was completely bamboolzed :thinking:. Why would we need an FString to start with when we are getting transform variables. At first I came up with code that crashed Unreal, I was feeling chuffed with myself for that, if it’s one thing I do well is crash computer programs. Anyway, came up with another way that was different to the crash method, it did use FString (??) and GetTransform(), but, it gave all the info for the transform again not what we were specifically told, which was to get the X, Y & Z values. Then because I was getting frustrated watched the solution and still I wasn’t satisfied. From the way the lecturer described what we were trying to do, I though we had be able to get to the single values on the X, Y & Z axis you know like ("x: %i, y: %i, z: %i ), xPos, yPos, zPos and was extremely annoyed when that wasn’t the solution. Another 2 hours later playing around with the Docs, Google, and Intellisense and swearing a little more loudly :scream:, and what I remembered from Unity, I finally found a method to extract the single values from Unreal and there was not an FString anywhere in sight - which caused all the problems for me in the first place.

I am so glad this is over :disappointed_relieved:. I am still of the opinion that getting the individual values is much more instructive than using the FString method(s).

It does seem complicated but with FVector, it’s just a matter of calling the ToString method which turns the vector into a formatted string for output. The only real reason I can see to have ToString on the FVector is for debugging/testing as was done here.

So UE_LOG(LogTemp,Warning,TEXT("Transform: %s"), *GetTransform().ToString()) does the same thing as getting the transform and extracting the X, Y and Z like so

FVector ActorTransform = GetTransform().ToString();
UE_LOG(LogTemp,Warning,TEXT("Transform: X:%f Y: %f Z: %f"), ActorTransform.X,ActorTransform.Y,ActorTransform.Z);

It doesn’t help the UE docs are a bit, and let’s be honest here, rubbish. They used to be better a few years back and I’m not sure what happened.

I hope this helps anyway.

EDIT: the X, Y and Z are floats. I saw you used %i which indicates integer.

Touché you got me there! I set the values as int32 after getting them from the FVector:

FVector myVector = GetOwner()->GetTransform().GetLocation();
int32 xPos = myVector.x

The compiler didn’t complain!?! I do that a lot, convert thing to values, if I’m going to do something with them, it allows my brain to follow what I’m doing a bit easier. And my coding style is about as far removed as you can get from what Unreal recommends :grin:. Started with BASIC, then some C, than Pascal, then JavaScript, Java, then C#, so I’m not about to change now :slight_smile:

I had no idea that the FString would do that, convert that FVector to a string and still let you print to the log as Floats, I will have to remember that. I didn’t know how to get to the individual values with the ToString() method in play.

Cheers.

It’s actually not FString. It’s FVector that does the converting. Also, it does all values. It’s really just a wrapper for doing it by hand.

The C++ Compiler doesn’t complain when doing the int conversion as it just truncates or performs a floor operation on the float and take the integer part. Usually with UE, that’s close enough as 1 unit=1cm unlike Unity which is 1 unit=1m

The UE classes often have helpers like ToString. There’s also the Kismet library which has converters from one type to another. Unfortunately, like I said, the docs are pretty poor.

Doh! One of the things writing messages early in the AM is that my thinking gets a little foggy :grin: - I saw ToString() immediately thought of FString! I should ban myself from writing message during that early.

One thing about floats which did throw an error was that I wrote: float newAngle = 90f; something I do without thinking, and without the decimal point would not compile.

I really have to say thankyou again for all your help.

Domo Arigato Gozaimashita. (from my days as Assistant Instructor in Martial Arts)

This gets me as well all the time. Rule of thumb, always include the .0 when you want to specify f so use 90.0f which is more obvious. If it’s an int value like 90, skip the f even. Personally, I like right-typed values.

Glad I can be of help.

Privacy & Terms