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

in Engine by (40 points)
edited by

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): 
       ..... 
by (40 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/

by (1,440 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.
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.