||Topic was automatically imported from the old Question2Answer platform.
- “background etc”
- GridNode2d (contains instances of PieceNode2d)
- KinematicBody2d ( can be clicked )
I would like to handle inputs cleanly within GridNode2d script where all other game related code is but it only has _input(event) function and I’m not sure if I can get clicked object from there?
Currently I have _input_event() in KinematicBody2d script where I can get which PieceNode2d was clicked (self…) but it somehow complicates further steps… code is not that elegant anymore
So how to get clicked object detected and handled in GridNode2d (which is parent of instantied PieceNode2d objects)?
So i am not sure if i understand your question correctly, but i think you just want to handle a click event right?
Well you should make a collisionnShape a child of an Area2D note. The Area2D note has signals, which detect is the mouse has entered or exited the collisionShape. So you can do something like:
var mouse_in = false
mouse_in = true
mouse_in = false
Then you need to check in the process function, whether the user clicked while the mouse was inside of the collision area:
if Input.is_action_pressed("mouse_button_left") and mouse_in == true:
You can configure and add new costum input references (like my “mouse_button_left”) at project settings → Input map.
The handling part can be done with signals. Create a signal called “clicked” or something in the instance and connect it to your GridNode2d via script after being instanced. There you can also put “self” as an argument, so GridNode2d can handle the event.
Hope this helped!
Question was more of what is better & cleaner way to handle click of instantiated object.
Top-down (parent handles all child object clicks) or botton-up (each children handles it’s own clicks and signals parent (or calls parent’s function)
Problem is that I don’t know internals of godot, is it big burden to engine if 200 child object handle their own clicked status or is it better to handle it from parent level (one _input handler, one event per click etc??)
I’m not sure if “mouse click of object” is even visible to parents… (or just visible to collision object)
InputEvent — Godot Engine (3.1) documentation in English
4 If no one wanted the event so far, and a Camera is assigned to the Viewport, a ray to the physics world (in the ray direction from the click) will be cast. If this ray hits an object, it will call the CollisionObject._input_event() function in the relevant physics object (bodies receive this callback by default, but areas do not. This can be configured through Area properties).
Godotmus | 2019-04-24 05:58
Probably the cleanest way of doing it is by handling it the way you currently are (with clickable bodies), and then signalling to parent when a pieces is clicked. I assume your parent is instancing the pieces, so the code might look something like:
For the piece:
And then the parent:
const Piece = preload(res://Piece.tscn)
var piece = Piece.instance()
piece.connect("clicked", self, "handle_piece_click")
#do something with your piece
It is also possible to have the pieces connect their own signals on _ready()
connect("clicked", get_parent(), "handle_piece_click")
But I prefer not to depend on my scenes knowing anything about their parent.
Thanks, looks like I was on the right track
Godotmus | 2019-04-25 07:17