Accesing mouse position/coordinates in C++

Hello

I have set up my mouse control according to this tutorial.

Great, except the tutorial finishes off with “When you play the map, you should now see the cursor. However, unless you have set up some sort of behavior (via C++ code or Blueprint), you will get no responses at this time” and is completely unhelpful in giving me hints on where to start with that.

I’ve been googling around trying to find something, but to no avail. The closest I could find was this, which destroyed my project.

What I wanna know is, how can I access my new class from my C++ pawn code and get and use the mouse coordinates? Getting them in 2D vector form would be ideal, but anything goes. As my project develops I will need to implement for complex mouse uses but for now I just need a starting point.

Can anyone help me please? Thanks!

You’ll want to use the GetMousePosition() method of the Player Controller. Here’s a simple example, where there is only a single PlayerController in the world:

float mouseX;
float mouseY;
UGameplayStatics::GetPlayerController(GetWorld(), 0)->GetMousePosition(mouseX, mouseY);
UE_LOG(MyLog, Warning, TEXT("Mouse Location: %f, %f"), mouseX, mouseY);

You’ll need these two libraries included in your .cpp file:

#include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"
#include "Runtime/Engine/Classes/GameFramework/PlayerController.h"

I am having some trouble with this. I’ve done exactly what you told to but every time I play the engine crashes