simple click 2d - how to tell when static body has been clicked

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By tproper

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?

:bust_in_silhouette: Reply From: SIsilicon
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.

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 _input_event() is working. This simple code doesn’t work:

func _input_event(viewport, event, shape_idx):

    if event is InputEventMouseButton:
	    print("i")

even with this set:

set_process_input(true)

tproper | 2018-10-14 22:35

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).

SIsilicon | 2018-10-15 00:09

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.

tproper | 2018-10-15 00:21

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.

SIsilicon | 2018-10-15 00:29

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():
	
	set_process_input(true)
	set_process(true)
	Drawer = get_parent().get_node('Drawer')
	
	connect("mouse_entered",self,"_on_mouse_entered")
	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:
			print('crossed')
		else: Crossed = 0

tproper | 2018-10-15 01:06

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!

tproper | 2018-10-15 15:06

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

SIsilicon | 2018-10-15 17:53

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 :smiley:

gianmarco | 2021-04-22 16:57