Mouse over events only recognized when standing inside actor's components?

I’m trying to get a function to execute when the cursor overlaps a box component on an actor. It hasn’t worked at all under normal conditions. However, it does work under non-normal circumstances: the player must be standing inside a component of the actor to get cursor overlap events to work. If the player is standing away from the object and not standing within any of the object’s components, it seems the cursor overlaps are not recognized. The code I am using is below. What could be causing such a problem?

In player controller constructor:

bEnableMouseOverEvents = true;

In actor’s header file:

UPROPERTY(EditAnywhere, Category = Testing)
UBoxComponent* BoxComponent;

UFUNCTION()
void MouseOverFunction(class UPrimitiveComponent* OtherComponent);

In actor’s constructor:

BoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxComponent”));
BoxComponent->AttachParent = TheParent;
BoxComponent->OnBeginCursorOver.AddDynamic(this, &AMyActor::MouseOverFunction);

Later in actor’s cpp file:

void AMyActor::MouseOverFunction(class UPrimitiveComponent* OtherComponent)
{
UE_LOG(LogTemp, Warning, TEXT(“Cursor has begun overlapping box component.”));
}

If I do not use the code above and instead simply override the NotifyActorBeginCursorOver to print a message to the log, the same behavior is still seen: the player must stand inside one of the actor’s components to execute cursor overlap events for that actor. Also, the problem persists even if objects and components are separated far from one another to ensure that nothing else is blocking the component that the cursor is over.

Hey pgr-

Mouse over events don’t register on box / capsule components. If you replace the box component with a static mesh component (and assign a static mesh in code or the editor) then your MouseOverFunction() should trigger appropriately.

Cheers

After replacing the box component with a static mesh component, the MouseOverFunction now never executes. The static mesh component is set to block all channels, but apparently doesn’t react to the cursor over it in such a way that MouseOverFunction executes. Is there something beyond what is listed in the question that needs to be done?

Using your code with a few edits to allow it to compile, the static mesh component appears to print properly at mouse overlap on my end. Here’s what I found that is working for me, let me know if you have success following the same setup.

In my custom player controller constructor I added:

bEnableMouseOverEvents = true;
bShowMouseCursor = true;

In the custom actor header:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Testing)
	UStaticMeshComponent* BoxComponent;

UFUNCTION()
	void MouseOverFunction(class UPrimitiveComponent* OtherComponent);

and in my actor.cpp (The GEngine lines require an include statement for Engine.h):

BoxComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BoxComponent"));
BoxComponent->AttachTo(RootComponent);
BoxComponent->OnBeginCursorOver.AddDynamic(this, &AMyActor::MouseOverFunction);

void AMyActor::MouseOverFunction(class UPrimitiveComponent* OtherComponent)
{
	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Mouse Overlap"));
	}
}

With this setup, creating a blueprint from my actor class and set a custom game more and my custom player controller. Playing in the editor printed the “Mouse Overlap” when hovering over the mesh. If you are still having issues after trying this setup please post the full class for your player controller and actor.

Still not getting cursor overlap events to be recognized. I completely started over, downloaded UE4.10, and made your suggested changes to a new project, but the problem still remains. Below are the exact steps that I tried. What is still incorrect about them?

First, I created a new project from the Shooter Game example project for UE 4.10, then used the editor’s C++ class wizard to add a new C++ class that was a child of the AActor class. In the wizard, the class was named CustomActor, and the target module was set to ShooterGame (Runtime).

I closed the editor and opened the newly generated Xcode workspace. The code was modified as described in the comment below this one. Then I went to Xcode’s menu bar, and selected Product > Build For > Profiling and built the project.

In the UE4 editor, I created a blueprint (BP) class based on CustomActor. In the BP class, I assigned a static mesh in Testing > Box Component > StaticMesh. In the BP class’s Testing > Box Component > Collision, I ensured that ‘Generate Overlap Events’ was ticked, that the Collision Presets was ‘Custom,’ and that every tick box had ‘Block’ selected. In the BP class’s Testing > Box Component > Collision > Collision Presets > Collision Enabled, the selection was set to ‘Collision Enabled (Query and Physics).’ In the BP class’s Testing > Capsule Root > Collision, everything was set identically to the Box Component collision settings. In the full BP editor, I changed the Capsule Root to a ~2 meter sphere, and the Capsule Root and Box Component were slightly separated from one another. I saved and compiled the BP class, and dragged it onto the map.

When the game is started in the PIE in the selected viewport, no overlap message ever appears when the mouse is overlapped with the BoxComponent. As far as I know, the player controller, game mode, player, and custom actor are otherwise set up as they should be. Is this not correct?

(See next comment for the code.)

(This code corresponds to the previous comment.)

In ShooterPlayerController.cpp, within AShooterPlayerController::AShooterPlayerController(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer), I put the following:

bEnableMouseOverEvents = true;
bShowMouseCursor = true;

In CustomActor.h, before the closing bracket but after virtual void Tick( float DeltaSeconds ) override;, I put the following:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Testing)
UStaticMeshComponent* BoxComponent;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Testing)
UCapsuleComponent* CapsuleRoot;

UFUNCTION()
void MouseOverFunction(class UPrimitiveComponent* OtherComponent);

In CustomActor.cpp, within ACustomActor::ACustomActor(), I put the following:

CapsuleRoot = CreateDefaultSubobject<UCapsuleComponent>(TEXT("CapsuleRoot"));
RootComponent = CapsuleRoot;

BoxComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("BoxComponent"));
BoxComponent->AttachTo(RootComponent);
BoxComponent->OnBeginCursorOver.AddDynamic(this, &ACustomActor::MouseOverFunction);
BoxComponent->SetCollisionResponseToChannel(ECC_Pawn, ECR_Overlap);

In CustomActor.cpp, after the definition of the tick function, I put the following:

void ACustomActor::MouseOverFunction(class UPrimitiveComponent* OtherComponent)
{
    if (GEngine)
    {
        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Mouse Overlap"));
    }
    UE_LOG(LogTemp, Warning, TEXT("Mouse overlap"));
    
}

CustomActor.cpp also had #include “Engine.h” included in the preprocessor directives.

The above code isn’t causing the “Mouse Overlap” message to appear when the cursor overlaps the BoxComponent.

Hey pgr-

After further testing it appears that what you’re seeing is being caused by the PlayerPawn set as the Default Pawn Class in the game mode. If you were to create a new blueprint based on GameMode and set the Default Pawn Class as “DefaultPawn” you should see your overlap messages appear as expected.