Hello! I’m trying to make a desktop companion game, similar to something like Desktop Defender or Ropuka’s Idle Island. I want the player to be able to right-click anywhere inside the window to start dragging it, but calling the built-in start_drag() function doesn’t seem to work. Here is my code thus far:
This script is attached to a default node inside my main scene. I’ve confirmed that the input is received, since replacing start_drag() with print(“drag”) will print a message. Also, sometimes the cursor will turn into a cross arrow for a split second, but this is inconsistent.
I’m on Linux Mint, is this an OS issue? Or is there something else I’m missing?
I programmed a drag and drop system for the card game 24 a few weeks ago. The code is here on Github.
Drag and drop systems can get complicated, depending on what features you want it to have. What happens on a drop? Where does the drop happen? What other inputs are allowed?
Essentially you need to do two things. 1) Store the dragged object at the start (in my code _start_drag stores the reference to the dragged object as drag_obj). And 2) you need to update the position of the dragged object according to the (mouse) pointer.
Looks very good to me. One thing that is particularly new to me is that you additionally detect for InputEventMouseMotion in your input. In my code I just assumed that between press and release the drag is being continued without continuously checking for motion and updating the position of the dragged object in the _process() function instead.
Your solution has the advantage that the code that handles the drag is all in one function, but also the order of the if statements matter, which one should not forget.
Your style with InputEventMouseMotion can also be found in the Godot docs as an input example, so I would as a default it is cleaner not to use the process function for the drag.