How to click and drag actors with mouse?

So I’m trying to implement a system where you can click and drag actors around with your mouse, like an RTS, but I’m having a lot of trouble getting any kind of working system.

My first idea was some kind of tick function, where you can click on an actor, it traces to your mouse position to find the actor, and sets the actor’s location to the location of the mouse every tick. But there are a lot of problems that have prevented me from doing this.

For starters, if the viewport captures my mouse, the mouse position won’t update until I release the click and I won’t be able to drag the actor. But if I set the capture mode to “no capture,” then it doesn’t register when I left-click. This could be solved by setting the input mode to “UI only,” but I have keybindings that need to be read that won’t register if the input mode isn’t “game and UI” or “game only.”

I’m really stumped with this and have completely run out of ideas. I’d really appreciate any help.

Thanks in advance!

Hey ,

Great question! Can you expand at all on the problems you face when running this function on tick? There is a great example of mouse behavior like this in one of the official content examples, specifically the Blueprints - Mouse Interaction level.

This is the relevant blueprint to make moving characters possible - note that this is in the actor being moved, not in the player controller.

I highly recommend poking through this content example. To access it, go to Unreal section of the Epic Games Launcher and click the Samples tab at the top. Then find the sample project called Content Examples and make a new project in the engine version of your choice. There are a ton of great examples in here!

I hope this information helps, and good luck!

3 Likes

Hey @ThatNerdFury,

Great question! Can you expand at all on the problems you face when running this function on tick?

Hi. So I’m not really running into any problems related to the tick function. Here are some examples of my failed attempts:

  1. If I let the viewport capture my mouse, it won’t update the mouse’s position, so I can’t move the selected actor anywhere. Here, you can see my mouse’s position printed on the screen each tick as I try to drag around the grey actor:

  2. If I disable mouse capture, my mouse inputs won’t get registered at all. In this first clip, mouse capture is enabled. In the second, mouse capture is disabled, and I try to click repeatedly, but it fails to register my left-clicks.

Capture Enabled:

Capture Disabled:

I’m really not sure where to go from here. I’ve looked at the sample project but it doesn’t have anything that could solve my problems here.

Hey @ThatNerdFury,

I need a few more pieces of information. First, can you go into your player controller settings and check your mouse settings in class defaults and see if capture click events and show cursor are enabled by default?

Second, can you show the blueprint(s) you are using for creating your click and drag effect? Can you disable all widgets and try this again, to see if a widget is somehow eating your inputs?

Finally, if you are setting Game and UI input manually somewhere, can you confirm that the checkbox on the boolean for “Hide cursor during capture” is NOT checked?

I look forward to hearing from you!

1 Like

I need a few more pieces of information. First, can you go into your player controller settings and check your mouse settings in class defaults and see if capture click events and show cursor are enabled by default?

“Show Mouse Cursor” and “Enable Click Events” are enabled by default.

Second, can you show the blueprint(s) you are using for creating your click and drag effect? Can you disable all widgets and try this again, to see if a widget is somehow eating your inputs?

I’m using this system for dragging and dropping, but instead of dropping the widget at a new location, I’m destroying the widget when the drop operation ends. When using the widget reflector, there is no widget being clicked. The default SViewport is the only reflected widget. I still tried disabling all of the widgets, but it didn’t have any kind of effect.

Finally, if you are setting Game and UI input manually somewhere, can you confirm that the checkbox on the boolean for “Hide cursor during capture” is NOT checked?

I actually wasn’t able to find this exact setting. I’ve found the settings for when to capture and when to lock in the project settings, but no setting named “hide cursor during capture.” Where is it exactly?

Lastly, I really appreciate you taking the time to help me out!

Hey @,

There is a bool in the Set Game and UI input blueprint node, shown here.
image
If that is left true, it doesn’t actually just set the cursor hidden - it also locks it in place.

If using C++, we would be looking for the line:

bool bHideCursorDuringCapture = true

In regards to your current system, detailed below,

I’m using this system for dragging and dropping, but instead of dropping the widget at a new location, I’m destroying the widget when the drop operation ends

Can you share any more details about this specific implementation?

Lastly, I really appreciate you taking the time to help me out!

My pleasure! I look forward to hearing from you again!

2 Likes

Aha, I see! I had been using SetInputMode(FInputModeGameAndUI()); to set the input mode, and didn’t even realize that it was setting MouseLockMode and bHideCursorDuringCapture behind the scenes. My only question now is where can I find the “bHideCursorDuringCapture” variable? The documentation says it’s in the FViewportClient, but I’m not sure how to access this. Could I just call UWidgetBlueprintLibrary::SetInputMode_GameAndUIEx()?

Hi @ThatNerdFury!

You can find bHideCursorDuringCapture in " SetInputMode_GameAndUIEx". Here is your syntax:

static void SetInputMode_GameAndUIEx
(
       APlayerController * PlayerController,
       UWidget * InWidgetToFocus,
       EMouseLockMode InMouseLockMode,
       bool bHideCursorDuringCapture
)

You can find more information here:

Unreal Engine Documentation: UWidgetBlueprintLibrary::SetInputMode_GameAndUIEx

If you are using “FInputModeGameAndUI”, I believe this is the syntax you are looking for:

FInputModeGameAndUI & SetHideCursorDuringCapture
(
       bool InHideCursorDuringCapture
)

You can find more information here:

Unreal Engine Documentation: FInputModeGameAndUI::SetHideCursorDuringCapture

If you would like to check whether or not the cursor is hidden when the viewport captures the mouse, you would use “FViewportClient” to check. Here is more information on that:

FViewportClient::HideCursorDuringCapture

I hope the above information is what you need to solve your problem!

1 Like

With this, I was finally able to get it to work. Thank you both so much for your help!