What I’m after is a simple kind of onMouseOver event (in C++) on Actors so when the Player points the reticule over an Actor and an event is fired to display text or other actions on the pointed at Actor, I can get the USphereComponent OnComponentOverlap to work on Actors but not sure how to go about what I’m after or even what event I’m needing to use, I’m not even sure if I should be setting this up on the Character Actor or the World Actor that is pointed at or maybe this is an event on the Character (First Person) UCameraComponent?
I hope somebody can shed some light on this issue for me.
Cheers
Hello, Troublesum
To implement MouseOver event, please do the following:
-
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);
-
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”));
}
}
-
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
Hi, I’ve tried doing this but removing the TouchedComponent parameter and changing the CustomOnBeginMouseOver names. It didn’t work. I assume it has to be like that yes or yes?