0 votes

I have a static body on the screen and I need to know if the user drags the mouse over the static body while the left mouse button is pressed. How can I do this?

in Engine by (107 points)
retagged by

1 Answer

+1 vote
Best answer
extends StaticBody2D

var can_drag = false

func _input_event(viewport, event, shape_idx):
    if event is InputEventMouseButton:
        can_drag = event.pressed

func _process(delta):
    if Input.is_mouse_button_pressed(BUTTON_LEFT) and can_drag:
        position = get_global_mouse_position()

You must enable Pickablein the StaticBody2D for this to work.

by (3,853 points)
selected by

I couldn't get this to work. You did understand that I don't want to click and drag the static body, instead I just need to know if the mouse is clicked and dragged across it.

Also, I'm not sure my inputevent() is working. This simple code doesn't work:

func _input_event(viewport, event, shape_idx):

    if event is InputEventMouseButton:

even with this set:


You enabled Pickable right? That function won't call if that's not enabled.

Also if you don't want to move them just replace that second if statement with:

drag_and_pressed = Input.is_mouse_button_pressed(BUTTON_LEFT) and can_drag

This becomes true when you hold your left mouse inside of the StaticBody2D(that includes dragging).

Okay, so that does work but only if you click on the static body and then drag. I'm trying to do something when you click anywhere then drag across the static body.

Basically, you draw a line with the mouse. It creates a bunch of static bodies in the form of the line and then I want drawing another line that crosses the old line to trigger an event to remove the old line (already have that event made). I just can't get anything to 'trigger' when I cross the line.

clicked and dragged across it.

Well then. When you put it that way .

You would need to detect when the mouse entered the static body, then exits it again.

All PhysicsBody nodes, including StaticBody, include two signals. mouse_entered and mouse_exited. You can connect the StaticBody's signals, these two, to itself. Then you could apply the following logic.

var went_across = 0.0 #when this is one, the mouse has gone across.

func on_mouse_entered(): #the method's name could be different.
    if Input.is_mouse_button_pressed(BUTTON_LEFT):
        went_across = 0.5 #It's in but it hasn't crossed yet.

func on_mouse_exited():
    if Input.is_mouse_button_pressed(BUTTON_LEFT):
        if went_across == 0.5:
            went_across = 1
        else: went_across = 0

func _process(delta):
    #Don't no if you want this, but this resets went_across when the left mouse is released.
    If not Input.is_mouse_button_pressed(BUTTON_LEFT):
        went_across = 0

Just came up with it, so test it.

Didn't know about those signals. Cool. But even with them connected and also adding them into the code (below), the signals aren't firing. I put breakpoints on both functions and it never hits them.

extends StaticBody2D

var Index
var Drawer
var Crossed = 0.0

func _ready():

    Drawer = get_parent().get_node('Drawer')

    connect("mouse_exited", self, "_on_mouse_exited")

func set_line_index(i):
    Index = i

func _process(delta):

    if not Input.is_mouse_button_pressed(BUTTON_LEFT):
        Crossed = 0

func _on_mouse_entered():
    if Input.is_mouse_button_pressed(BUTTON_LEFT):
        Crossed = 0.5

func _on_mouse_exited():
    if Input.is_mouse_button_pressed(BUTTON_LEFT):
        if Crossed == 0.5:
        else: Crossed = 0

Figured out why it wasn't working. Just what you said, the pickable has to be set with those signals too. Thanks for your help!

Glad I could help. And thanks for selecting my answer. :-)

Hi, what exactly do you mean with this? I am a beginner and as far as I know ray_pickable is set for the node, so the phrase "set it with those signals too" confuses me :D

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.