2D - How to know when the cursor is over a script generated child node

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

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

:bust_in_silhouette: Reply From: MysteryGM

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: Reddit - Dive into anything

To see what event types you can use check: InputEvent — Godot Engine (3.0) documentation in English

MysteryGM | 2018-10-27 23:24

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

YP | 2018-10-28 10:06

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.

MysteryGM | 2018-10-29 22:11

:bust_in_silhouette: Reply From: YP

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