I would say you don’t get the same pawn, maybe spectator ? but those value looks odd, could you log the pawn object name ? for both of those, just to be sure.
right now code doesn’t looks bad/wrong ( no pointer check, but that should crash and not give random results )
You’re using %d while vector’s components are floats. Try to use %f for the UE_LOG formatting.
Or you can log out the whole vector via the string (%s): *YourPawn->GetActorLocation().ToString() ← don’t forget the asterisk.
Anyway, it all depends on your actual code. If you’re doing it in blueprints as you said, then it’s better to post a screenshot or https://blueprintue.com/ link with the example.
If you doing it in the code, then you can post your exact code, to see from where you’re calling it and so on.
For futher debug, your can log out the pawn’s name to see if it’s the correct actor.
If you make this an answer instead of a comment I can click the doobly doo and resolve! Edit contains additional information based on your comment to resolve the issue.
In this instance, I am attempting to locate the player’s pawn in relation to the actor Foo. In Foo, running the following commands returning unexpected results when attempting to locate the player’s pawn. I wrote some blueprint code which gives the expected result. Spent a few hours on this, any leads would be appreciated!
Currently running the Side Scroller Template with the default player character and a simple Foo Actor placed into the world.
Expected: X: 0.0 | Y: -907.908142 | Z: 400.262909
Actual: X: 0 | Y: -536870912 | Z: -536870912
Actual: X: 0 | Y: -1610612736 | Z: -536870912
Actual: X: 0 | Y: -2147483648 | Z: -1073741824
FVector target = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();
FVector target = UGameplayStatics::GetPlayerPawn(GetWorld(), 0)->GetActorLocation();
UE_LOG(LogTemp, Warning, TEXT("X: %d | Y: %d | Z: %d"), target.X, target.Y, target.Z);
[EDIT]
After further investigating this code based on the comments below, it appears that when using %d instead of %f when formatting this string is what caused the weird response. The odd thing is, this originally came up because the GetDistanceTo was giving me absurd numbers, which I’m not able to replicate now.
The full cpp file is listed below for reference.
#include "Foo.h"
#include "Engine/World.h"
#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
// Sets default values
AFoo::AFoo()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AFoo::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AFoo::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
APawn* act1 = GetWorld()->GetFirstPlayerController()->GetPawn();
FVector vec2 = act1->GetActorLocation();
UE_LOG(LogTemp, Warning, TEXT("%s"), *act1->GetFName().ToString());
UE_LOG(LogTemp, Warning, TEXT("%s"), *act1->GetActorLocation().ToString());
UE_LOG(LogTemp, Warning, TEXT("2 as float | X: %f | Y: %f | Z: %f"), vec2.X, vec2.Y, vec2.Z);
UE_LOG(LogTemp, Warning, TEXT("2 as decim | X: %d | Y: %d | Z: %d"), vec2.X, vec2.Y, vec2.Z);
FVector vec1 = UGameplayStatics::GetPlayerPawn(GetWorld(), 0)->GetActorLocation();
UE_LOG(LogTemp, Warning, TEXT("1 as float | X: %f | Y: %f | Z: %f"), vec1.X, vec1.Y, vec1.Z);
UE_LOG(LogTemp, Warning, TEXT("1 as decim | X: %d | Y: %d | Z: %d"), vec1.X, vec1.Y, vec1.Z);
float result = GetDistanceTo(act1);
UE_LOG(LogTemp, Warning, TEXT("Distance to actor: %f"), result);
}
Output of the above code example:
LogTemp: Warning: SideScrollerCharacter_2
LogTemp: Warning: X=0.000 Y=-254.985 Z=205.944
LogTemp: Warning: 2 as float | X: 0.000000 | Y: -254.984894 | Z: 205.944321
LogTemp: Warning: 2 as decim | X: 0 | Y: 1073741824 | Z: -536870912
LogTemp: Warning: 1 as float | X: 0.000000 | Y: -254.984894 | Z: 205.944321
LogTemp: Warning: 1 as decim | X: 0 | Y: 1073741824 | Z: -536870912
LogTemp: Warning: Distance to actor: 517.060425