How do i get the player position from c++ script?

Hello,
I’m trying to get my c ++ character to go to the player position which is a BP but I don’t know how to do it.
How do I get the position of the player?
And from there how do I move the character that contains the c ++ script to the player position?
Thank you !

You get the location of any actor with

actor->GetActorLocation()

What do you mean the player position is in a BP ? The actor has a world position, just get that.

By BP I mean that the player is not my c ++ script, c ++ is my boss, the player is a separated Blueprint
Yes, but the actor has to be the player.
How can I make “actor” my player?
Thank you for the answer!

Is your c++ based character an AI that should move to the players location?

Getting the players location is easy enough, just get a reference to the actor and then ->GetActorLocation(), for example make a UPROPERTY(EditAnywhere) AActor* player; and set that in the details of your c++ character.

If its an AI and you want to handle it all through cpp then you probably need c++ AI Contrller class that possesses your c++ character. In the AI Controller class there are a bunch of Move To Location functions.
You can also make a BP AI Controller class if you want to make it easy for yourself

GetPlayerController in BP is UGameplayStatics::GetPlayerController in C++
GetGameState in BP is UGameplayStatics::GetGameState in C++

If you are asking how to get the player from your boss blueprint, you could use GetPlayerController if it’s single player. If there might be multiple players, you can get the player array from the game state, and pick whatever one is relevant.

Under normal game circumstances though this selection would be part of some manner of perception and/or target selection though for AI.

Sorry, my English is not very good, you have not understood me:
The player → BP
The Boss → C ++
And I want to create a function called “movetolocation” that moves my Boss (c ++) to the position of my Player (BP).
To get the position of my Player (BP) from my Boss (c ++) what would I have to do?

I finally got it !!!
Thank you so much guys !

void ABoss_Character::getPlayerPosition() {

FVector MyCharacterPosition = GetWorld()->GetFirstPlayerController()->GetPawn()->GetActorLocation();

GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("Player Location: %s"), *MyCharacterPosition.ToString()));

}

This is the function in case someone needs it in the future

1 Like

Great, don’t forget to mark as Solved so if anyone else has a similar issue they quickly see that you found a solution

No this is not a good way to get the players position. Yes this will work in a single player scenario but there is no such thing as single player in Unreal, single player is a LAN listening server not actual single player. You should get a reference to your players pawn or actor some other way. First question to ask yourself is when does your boss start going to the players position? Is it when the boss can see the player, or when the player is in a certain distance of the boss etc. You need some way to detect the players actor/pawn. AIPerception system is most likely what you are looking for. Give your boss an AIPerception component and give your player an AIPerception Stimuli component. Then you can use the OnTargetPerceptionUpdated() function which will give a reference to the player’s actor. Or you can use GetFirstPlayerController() but that will always get the controller of the first player to join the game when you call it on the server/host… Very bad practice because you are locking your game as single player and will get a flood of problems if you want to make it multiplayer or even just co op.