Position report with X,Y,Z coordinates

Here’s my implementation

// Called when the game starts
void UPositionReport::BeginPlay()
{
    Super::BeginPlay();
#
    FString ObjectName = GetOwner()->GetName();
    FVector Location = GetOwner()->GetActorLocation();
    FString ObjectPosition = FString::Printf(TEXT("X=%g, Y=%g, Z=%g"),Location.X,Location.Y,Location.Z);
    UE_LOG(LogTemp, Warning, TEXT("Position report for %s at %s"), *ObjectName,*ObjectPosition);

    // ...
    
}

There is a handy method in FVectors for printing out the position:

Location.ToString()

UE_LOG(LogTemp, Warning, TEXT("Position report for %s at %s"), *ObjectName,*Location.ToString());

That way you could get rid of the ObjectPosition line :slight_smile:


:+1: Fail Faster - there is no instant win :+1:
Twitter: @GamedevCala - Blog: nerd-time.com - Twitch: GamedevCala

This is how I did it:

void UPositionReport::BeginPlay()
{
    Super::BeginPlay();

    // ...
    //Get the name of the object this script is on
    FString ObjectName = GetOwner()->GetName();
    FString ObjectPos = GetOwner()->GetTargetLocation().ToString();
    UE_LOG(LogTemp, Warning, TEXT("%s is at %s"), *ObjectName, *ObjectPos);
}

Here is the code that I used:

FString ObjectName = GetOwner()->GetName();
FString ObjectPos = GetOwner()->GetActorLocation().ToString();
UE_LOG(LogTemp, Warning, TEXT("%s is at %s"), *ObjectName, *ObjectPos);

It seems to be easier than going through GetTransform() and then pulling out the location. It gave me the correct values so I hope it is still the same as what Ben did.

Either way is good, I think I learned more doing it the way I did than if I had just done .ToString() . There’s always several different ways to achieve the same result. I can also see exactly what is going to be output and modify it if I just wanted x,y,z for example

I used the same and seem to provide the same output that Ben’s code had. I wonder if there is any long term issues? Like larger projects with many objects?

FString ObjectName = GetOwner()->GetName();
FString ObjectPos = GetOwner()->GetActorLocation().ToString();
UE_LOG(LogTemp, Warning, TEXT("%s is at position: %s"), *ObjectName, *ObjectPos);[quote="Stephen_Jenkins, post:1, topic:1990, full:true"]

Here’s my implementation

// Called when the game starts
void UPositionReport::BeginPlay()
{
    Super::BeginPlay();
#
    FString ObjectName = GetOwner()->GetName();
    FVector Location = GetOwner()->GetActorLocation();
    FString ObjectPosition = FString::Printf(TEXT("X=%g, Y=%g, Z=%g"),Location.X,Location.Y,Location.Z);
    UE_LOG(LogTemp, Warning, TEXT("Position report for %s at %s"), *ObjectName,*ObjectPosition);

    // ...
    
}

[/quote]