Paper2D TileMap Mouse Event Handling?

I’m playing around with a Paper2D TileMap for a 2D strategy game where the player clicks on units and then on the map to give them a destination. All very, very simple.
The map is being rendered using orthographic projection and I have set up collision shapes for all the tiles in the tileset.

Currently I’m stuck at getting mouse move and click events to correctly identify which tile the mouse is over and I cannot seem to find any good tutorials/examples that show how to do this.

Does anybody know how to:

  • setup a Blueprint script that prints the tile coordinates over which the mouse hovers when it enters a tile?
  • setup a Blueprint script that handles a mouse click event identifying the tile coordinates over which the user clicked the mouse button?

Thanks, George

to your question " * setup a Blueprint script that handles a mouse click event identifying the tile coordinates over which the user clicked the mouse button?"
I did it like this i hope it helps someone in the future

1 Like

Enable Mouse Over Events. Not sure if you figured this out yet, but I wanted to post the answer in case someone was looking for it.

Enable mouse over events and… what? Enabling mouse over does nothing in and of itself.

I made a thin C++ wrapper around UPaperTileMap::GetTileCoordinatesFromLocalSpacePosition
Mouse events just bypass the AActor handlers, so im using Left Mouse Button Event + Convert Mouse Location

// UE 5.5.4 
// Add Paper2D dependency to *.uproject
"AdditionalDependencies": [ 
    // ...
    "Paper2D" 
]

// Add to *.Build.cs
PublicDependencyModuleNames.AddRange(new string[] { 
    //...
     "Paper2D"
});
 
// ChessBoardActor.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "PaperTileMapActor.h"
#include "ChessBoardActor.generated.h"

UCLASS()
class CHESSDRA_API AChessBoardActor : public AActor
{
	GENERATED_BODY()
	
public:	
	AChessBoardActor();

public:	
	UFUNCTION(BlueprintCallable)
	void GetTileIndexAtLocation(const FVector InputLocation, int32& OutputX, int32& OutputY);

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"));
	TObjectPtr<class UPaperTileMapComponent> TileMapComponent;
};
// ChessBoardActor.cpp
#include "ChessBoardActor.h"
#include "PaperTileMapActor.h"
#include "PaperTileMap.h"
#include "PaperTileMapComponent.h"

AChessBoardActor::AChessBoardActor()
{
	TileMapComponent = CreateDefaultSubobject<UPaperTileMapComponent>(TEXT("RenderComponent"));
	RootComponent = TileMapComponent;
    //NOTE: replace TM_Board with your asset!
	static ConstructorHelpers::FObjectFinder<UPaperTileMap> Board(TEXT("/Game/2D/TM_Board"));
	if (Board.Succeeded())
	{
		TileMapComponent->SetTileMap(Board.Object);
	}
}

void AChessBoardActor::GetTileIndexAtLocation(FVector InputLocation, int32& OutputX, int32& OutputY)
{
	//NOTE: Board must be placed at 0,0,0 location
	TileMapComponent->TileMap->GetTileCoordinatesFromLocalSpacePosition(InputLocation, OutputX, OutputY);
}