Can not drag UUserWidget

Dear experts,

I am trying to make an inventory slot (UUserWidget) I can drag out of the inventory
and drop it to get rid of an inventory item.

This is my .cpp file:

//constructor
UInventorySlot::UInventorySlot(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	
}

void UInventorySlot::NativeConstruct()
{
	Super::NativeConstruct();
}

FReply UInventorySlot::NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
	Super::NativeOnMouseButtonDown(InGeometry, InMouseEvent);

	const FEventReply reply = UWidgetBlueprintLibrary::DetectDragIfPressed(InMouseEvent, this, EKeys::LeftMouseButton);
	const FVector2D DragOffset = InGeometry.AbsoluteToLocal(InMouseEvent.GetScreenSpacePosition());
	GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Green, TEXT("Drag: On Mouse Button Down"));
	return reply.NativeReply;
}

void UInventorySlot::NativeOnDragDetected(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent,
	UDragDropOperation*& OutOperation)
{
	Super::NativeOnDragDetected(InGeometry, InMouseEvent, OutOperation);
	GEngine->AddOnScreenDebugMessage(-1, 3.f, FColor::Green, TEXT("NativeOnDragDetected"));

	const auto DragVisual = CreateWidget<UInventorySlot>(this, UInventorySlot::StaticClass());
	DragDropOperation->DefaultDragVisual = DragVisual;
	DragDropOperation->Payload = this;
	DragDropOperation->Pivot = EDragPivot::MouseDown;
	DragDropOperation->Offset = InGeometry.AbsoluteToLocal(InMouseEvent.GetScreenSpacePosition());
	if(DragVisual)
	{
		
		DragVisual->AddToViewport();
		
	}
	OutOperation = DragDropOperation;
}

I can see my inventory slots (I have Blueprints also) but if I click on one I can’t drag it. Also the ScreenDebugMessages of methods

FReply UInventorySlot::NativeOnMouseButtonDown
void UInventorySlot::NativeOnDragDetected

are not shown on the screen.

Please help, what am I doing wrong?

Best regards, Peter

Creating Drag and Drop UI | Unreal Engine Documentation should give you some idea.

Looks like you’ve basically followed that though.

Since there’s an on mouse button down, do your widgets have visibility set to visible? Otherwise they won’t be able to get mouse events. Also make sure the parent widget doesn’t have visible otherwise the slots won’t get the events

Hi cd1232,

thank you for your link, I was following that already.
I figured it out: I had reparented the widget to the UE4 base class to test something
I parented it to my C++ again and it looks better.

However when I drag the mouse the widget is not moving.

So I’ve found that if I follow the blueprint way it works. I think it’s something to do with the Create Drag Drop Operation blueprint node. Not sure how you went about that in C++ but I’m guessing it does something special

Somehow I have to set the position for the widget I am moving I think. I have to look into that and see what else in the blueprint they are doing

Hey I figured out the problem. It’s this line

//NewDragDrop->Offset = InGeometry.AbsoluteToLocal(InMouseEvent.GetScreenSpacePosition());

Without it it works normally. Also don’t need to have

//const FVector2D DragOffset = InGeometry.AbsoluteToLocal(InMouseEvent.GetScreenSpacePosition());

either, since it’s not actually assigned to anything.

Also for the widget that is the DragVisual usually you would create a simplified version of the widget you’re dragging (like just an image or something), because obviously the actual widget might have lots of functionality you don’t want

thank you again. yes, it now works for me too! I just have to create the drag widget or DragVisual to see what I am dragging. I will update you on this.