Getting mouse position with a catch

Is it possible if yes how can I get the mouse position within an actor class in c++ ?

Hi! Tseno. Check this:

void AMyActor::Tick(float DeltaTime)

    if (GetWorld())
        FVector2D pos;

        UE_LOG(LogTemp, Warning, TEXT("x:%f y:%f"), pos.X, pos.Y);
        if (GEngine)
            GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Blue, FString::Printf(TEXT("x:%f y:%f"), pos.X, pos.Y));

Hope this helps!

Hi Andrew

I managed to do it myself like this including the Engine.h file

FVector2D mouse_coordinates;

Which also works, but now looking at your code I’m not sure if its the best way to do it, thanks a for the help!
Also another thing, do you know how to implement mouse input from inside the actor? Currently i’m including Windows.h and using GetAsyncKeyState(VK_LBUTTON) to determine if mouse left click has occurred just because it’s easy. (And to be honest am wondering if I should leave it like that because I’m not worried about compatibility)

Hi Tseno. If you don’t care about crossplatform development you can use WinAPI.

But maybe you need clicked (Mouse Left) on actor-cube in game:

  1. First create custom PlayerController (BP or C++) and in MouseInterface check Enable Click Events…seControlSetup

  2. MyGameBaseMode.cpp find and set custom PlayerController:

    : Super()
    static ConstructorHelpers::FClassFinder<APlayerController> PlayerControllerClassFinder(TEXT("/Game/My_PlayerController"));
    PlayerControllerClass = PlayerControllerClassFinder.Class;

In ProjectSettings → Maps&Modes → Default Modes → Default GameMode set MyGameBaseMode

  1. **MyActor.cpp **and call function (AMyActor::OnClickedCall) from dynemic multi-cast delegate…egates/Dynamic

void AMyActor::BeginPlay()
    // ...
    UStaticMeshComponent *CustomMeshComponent = FindComponentByClass<UStaticMeshComponent>(); // drag-drop cube in ueditor for test
    if (CustomMeshComponent)
        UE_LOG(LogTemp, Warning, TEXT("AMyActor::AMyActor CustomMeshComponent"));
        CustomMeshComponent->OnClicked.AddDynamic(this, &AMyActor::OnClickedCall);

line 157 about signature delegate

DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FComponentOnClickedSignature, UPrimitiveComponent*, TouchedComponent , FKey, ButtonPressed);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams( FComponentOnReleasedSignature, UPrimitiveComponent*, TouchedComponent, FKey, ButtonReleased);

line 897 OnClicked, OnReleased info
Event called when the left mouse button is clicked/released while the mouse is over this component and click events are enabled in the player controller */

void AMyActor::OnClickedCall(UPrimitiveComponent* TouchedComponent, FKey ButtonPressed)
    UE_LOG(LogTemp, Warning, TEXT("AMyActor::OnClickedCall"));


#include "Runtime/Engine/Classes/Components/PrimitiveComponent.h"
#include "Runtime/Core/Public/Delegates/Delegate.h"

class MYPROJECTTHREAD_API AMyActor : public AActor
        void OnClickedCall(UPrimitiveComponent* TouchedComponent, FKey ButtonPressed)

**In addition **check Bindings:…wTo/SetUpInput

Hope this helps. Good luck!