INPUT_EVENT only on a node inside an area 2d ? (picture)

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

Is it possible to “input_event” a specific sprite inside an area2d ?
For example: my tower has a RANGE collision and it TOWER_PICTURE sprite.

When I input_event it obviously include the range.
But I wish to make it work only on the SPRITE ( the #2 on the picture)

Is there an easy way to attribue that input_event ONLY to it ?

:bust_in_silhouette: Reply From: kidscancode

Add a second collision shape to the area. One of the parameters you’re passed with the input_event signal is the shape_idx which identifies which shape collected the input.

:bust_in_silhouette: Reply From: jgodfrey

If I understand the question, you want to process a mouse click event, but only when it’s on the tower icon instead of anywhere inside the collision shape. If that’s the case, you can just include 2 CollisionShape2D nodes (one for the range, and one just covering the tower icon) like this. That could look like this in the scene tree.

Tower (Area2D)
  - range (ColliisonShape2D for tower range)
  - click (CollisionShape2D for mouse click)

Then, in your input handler, you just need to check the value of the passed shape_idx argument, which indicates which collisionshape was clicked). So, something like:

func _on_Area2D_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.pressed && shape_idx == 1:
		print("Tower clicked")

Note, the clicking on the “range” collision shape will set shape_idx to 0 and clicking on the click collision shape will set shape_idx to 1

Got it ! Thank you very much.
Im assuming 0 and 1 are the order of the layers being played out ? Like if I were to add a 3rd collision shape it would be 2 ?

Thank !

quizzcode | 2020-05-15 23:12

Yeah, the indices will be from 0-N (based on the number of collision shapes associated with the area2d) and will be in the order they’re shown in the scene tree (with the upper-most node being 0 and the bottom-most node being “N”).

jgodfrey | 2020-05-15 23:17