0 votes

I have a randomly generated 2d tilemap and children nodes (Control, Sprite, etc.) that are added to it through script, like this:

var player = preload('res://Characters/Player/Player.tscn').instance()
add_child(player)

Some of them are Area2D, others Control etc.
I want to know when the cursor is over any of the children, and also get info about it (ID, Object, etc...)

asked Oct 27, 2018 in Engine by YP (38 points)
edited Oct 28, 2018 by YP

2 Answers

0 votes
Best answer

The answer is that I have to connect them:

Area2D.connect('input_event', self, 'callback', [Area2D])

func callback():
       ......

So something like this is not necessary:

func _on_Area2D_input_event(viewport, ev, shape_idx): 
       ..... 
answered Oct 29, 2018 by YP (38 points)
+1 vote

Area2D and other collision types has a signal called "input event". This signal can be called to check events, like this:

func _on_Area2D_input_event(viewport, ev, shape_idx):  
    if ev is InputEventMouseButton and ev.doubleclick:
        print("Double Clicked!")

This way the child can tell the parent when clicked or do what ever is needed. It only needs one of the collider types.

You will see when you make the signal the properties will be (viewport, event, shape_idx), I just made it ev so you can see where I get it.

I hope this helps, I learned it from: https://www.reddit.com/r/godot/comments/9jjro2/how_to_detet_mouse_clicks_in_ui_element/

answered Oct 27, 2018 by MysteryGM (1,426 points)

This doesn't seem to work, I've tried both _on_Area2D_input_event(viewport, ev, shape_idx)
and _on_Area2D_mouse_entered(ev) and it prints nothing.
I'm talking about a Area2D child

I'm talking about a Area2D child

Each child should have their own Area2D or collision type, if you want to click on it.
To be clicked on it needs something that can be clicked on.

edit:
Just to check that we are on the same page. The example above is a signal, it is no use if you directly copy and paste the code; you need to connect that signal.

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.