Getting Actor's Transforms? Somewhat

I was able to do part of the Challenge for section 4.94. The main issueI ran into was that I did not realize I had to place a .ToString() at the end in order to make the code work. A secondary issue, quite minor, was dealing with the * operator. I had two on ObjectName and it crashed the system.

However, had I placed the .ToString() bit into my code, the rest would have worked it self out.

Here is the for WorldPosition.CPP, notes and all:

UWorldPosition::UWorldPosition()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	// ...
}


// Called when the game starts
void UWorldPosition::BeginPlay()
{
	Super::BeginPlay();


	FString ObjectName = GetOwner()->GetName();
	FString ObjectPosition = GetOwner()->GetActorLocation().ToString(); //IS Shorter than below.
	
	// FString ObjectPosition = GetOwner()->GetTransform().GetLocation().ToString(); will give the location only
	/* .ToString changes everything on the other side of the '=' sign to an FString, so 
	 To match the otherside "FString ObjectPosition" */

	// UE_LOG(LogTemp, Warning, TEXT("Object name is: %s"), **ObjectName);
	UE_LOG(LogTemp, Warning, TEXT("%s is located at: %s"), *ObjectName, *ObjectPosition);


	/* 
		This is a second way to do the above
	UE_LOG(LogTemp, Warning, TEXT("Object name is: %s"), *GetOwner()->GetName());
	 
	Above can lead to calculating distance from us to another object, allowing for a radius
	around the player, and can lead to help pick up items at a certain distance.
	*/
	
}

I had originally tried using the GetTransform() member. It came to life once I learned about the .ToString method. The instructor also noted that you could place a .GetLocation() afterwards if all we wanted was the X,Y,Z.

What I have learned:

  1. .ToString() is useful for converting the code on the left side of the the equal sign into a string, especially if the declared variable is an FString. Now both sides equal out. Now I have a useful FString that I can call up, and dereference it for outputting the string.

  2. Double asterix ** attached to the variable can crash Unreal, but the * did not.

2 Likes

Great job for figuring it out and great job learning ToString() and asterix** :+1:

Privacy & Terms