# Godot, hide objects

Currently the way I do it, is setup an Area2d and a collision shape child for each individual node and then I attach a script to the area2d

extends Area2D

func _input_event(viewport, event, shape_idx):
if event.type == InputEvent.MOUSE_BUTTON \
and event.button_index == BUTTON_LEFT \
and event.pressed:
sorite.hide()


Is there a faster way? suppose I want to hide each object the user clicks on, and only then I want to allow him to press on SpriteX And I have a trillion object that can be clicked, what to do?

• I believe step 1 is to not have a trillion objects. In fact, I highly doubt that you do have a trillion objects. If this is not a problem don't prematurely optimize :) – Charanor Jul 29 '17 at 20:56

As Charanor stated correctly, you should not worry too much about optimising your code before you actually get into trouble. From my experience the slowest functions are never the ones you would expect :-)

Anyways, Godot offers a nice way to handle inputs using its InputMap. With the InputMap you can define actions and link them to different keys. In the long run it makes it way easier to change keys in the game.

You can find an Introduction to the InputMap in the Pong Tutorial.

For your example I would connect the Area2D-InputEvent Signal (of the Sprite) to your script (using the editor as described here). And then simply add the following code to the generated function.

func _on_Area2D_input_event( viewport, event, shape_idx ):
if event.is_action_pressed("mouse_left"):
sorite.hide()


Although I am not sure if it is faster it is way cleaner and as mentioned easier to change later as you just need to change the Key/Button in the inputmap.

• cant do that, you're supposed to finding stuff by touching them on the phone, so no keys to map, just touch. the mouse is used here for testing – Lynob Aug 1 '17 at 19:28
• Okay, in this case the InputMap wouldn't be needed. Nevertheless, I think using the _on_Area2D_input_event() function is an easier way to track if the correct object was touched and you don't need to do this "manually". – magenulcus Aug 1 '17 at 21:14
• wouldn't I need to create a separate area2d and a collision shape for each object like I'm currently doing? – Lynob Aug 2 '17 at 10:18
• Sorry my fault. I was typing the answer from my phone and wasn't paying attention to you code snippet and thought you were using the input(event) function. Actually your code and mine are pretty similar. Have you tried your game already? Is it working fast enough? – magenulcus Aug 2 '17 at 18:11