0 votes

Is it possible to "inputevent" 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 ?

in Engine by (184 points)

2 Answers

+2 votes
Best answer

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

by (19,268 points)
selected by

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 !

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

0 votes

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.

by (21,973 points)
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 Frequently asked questions and 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 [email protected] with your username.