Interacting with Actors MouseOver event? C++

Hello, Troublesum

To implement MouseOver event, please do the following:

  1. In Actor’s header, add these lines:

     //Static Mesh Component to interact with
    

    UPROPERTY(VisibleAnywhere)
    UStaticMeshComponent* CustomMeshComponent;

    //Function to handle the interaction
    

    UFUNCTION()
    void CustomOnBeginMouseOver(UPrimitiveComponent* TouchedComponent);

  2. Define the function in Actor’s .cpp file:

    void AMyActor::CustomOnBeginMouseOver(UPrimitiveComponent* TouchedComponent)
    {
    if (GEngine)
    {
    GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Cyan, TEXT(“Mouse Over”));
    }
    }

  3. Initialize the component and bind the function in Actor’s constructor:

    CustomMeshComponent = CreateDefaultSubobject(TEXT(“Test Component”));
    CustomMeshComponent->AttachTo(RootComponent);
    CustomMeshComponent->OnBeginCursorOver.AddDynamic(this, &AMyActor::CustomOnBeginMouseOver);
    Finally, create a Player Controller to control mouse. (If you like to learn more about creating Player Controller to control mouse, please go here:

Content Examples Sample Project for Unreal Engine | Unreal Engine 5.1 Documentation)

This should do it. Please don’t forget to place the Actor into the level and add the Static Mesh asset.

Hope this helped!

Have a great day!

1 Like