+1 vote

Hi!
I'm trying to do a simple drag and drop, that's why, following some samples, I'm using a connection of an Area2D input_event.
I do it in an simple scene to be instanced later in the main scene.

The scene tree:
Node2D
--Sprite (also tried out: TextureRect and Button)
--Area2D
----CollisionShape2D

Then I connect, by nodo tab, the CollisionObject2D.input_event from the Area2D to this function:

func _on_area_input_event( viewport, event, shape_idx ):
if event.is_action_pressed("left_click"):
    dragged = true
    print("mouse_down")
if event.is_action_released("left_click"):
    dragged = false
    print("mouse_up")

After that, I set the _process event true, and in the function:

func _process(delta):
#if Input.is_action_pressed("left_click"):
#   dragged = true
#if Input.is_action_just_released("left_click"):
#   dragged = false
if dragged:
    var xoffset = get_node("img").rect_size.x/2
    var yoffset = get_node("img").rect_size.y/2
    var pos = Vector2(get_global_mouse_position().x-xoffset,
                    get_global_mouse_position().y-yoffset)
    self.set_global_position(pos)

Well, if I play this scene by its own, It works perfectly, I can drag the image and drop it.

Note: As you can see, after nothing worked out, I tried the comment lines, without handling input_event. It works, but if you instance more than one copy, the move togheter at the same time, hahaha!

Then, in the main scene, I instantiate the scene by doing:

var img = load("res://scenes/draggableImage.tscn").instance()
get_node("background").add_child(img)

(background is a TextureRect, also tried with Sprite)

It's added and it's visible for me when playing the main scene, other things like printing a string works fine, but it's not processing the inputevent anymore (even tested with print() inside the "onareainput_event" function)

I don't know why is it happening, I'm getting crazy!
Is it a bug? Should I report it?

Also, I have another minor but annoying problem.
When adding multiple instanced (draggableImage) scene into a VBoxContainer, they stack one over the other, instead of being placed one after the other in column shape.

in Engine by (327 points)

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.