[Original tutorial is based on UE 4.18, MINE is based on UE 4.25]

English original Address

Following the next section, this is a simple tutorial on how to get the player’s current (vector) position from other characters. Inherit a new class called FindPlayerLocation from the Actor parent. We don’t need to do anything in the header file.

Here is the default header file generated for the class.

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "FindPlayerPosition.generated.h"

UCLASS(a)class UNREALCPP_API AFindPlayerPosition : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	AFindPlayerPosition(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};
Copy the code

In the.cpp file, we’ll put the logic in the Tick function of the Actor-derived class. Get the player’s location using the GetWorld() function that each character can access, then GetFirstPlayerController(), then GetPawn(), then GetActorLocation().

Here is the last function we will call. We pass the return vector to a variable named MyCharacter.

FVector MyCharacter = GetWorld() - >GetFirstPlayerController() - >GetPawn() - >GetActorLocation(a);Copy the code

Here is the final.cpp file. I added a DebugMessage to print the location of our player every frame on the screen.

#include "FindPlayerPosition.h"


// Sets default values
AFindPlayerPosition::AFindPlayerPosition()
{
 	// 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 AFindPlayerPosition::BeginPlay(a)
{
	Super::BeginPlay(a); }// Called every frame
void AFindPlayerPosition::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	// get first player pawn location
	FVector MyCharacter = GetWorld() - >GetFirstPlayerController() - >GetPawn() - >GetActorLocation(a);// screen log player location
	GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Blue, FString::Printf(TEXT("Player Location: %s"), *MyCharacter.ToString()));	

}
Copy the code

Final rendering