3
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Charanor Jul 29 '17 at 20:56
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Lynob Aug 1 '17 at 19:28
  • \$\begingroup\$ 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". \$\endgroup\$ – magenulcus Aug 1 '17 at 21:14
  • \$\begingroup\$ wouldn't I need to create a separate area2d and a collision shape for each object like I'm currently doing? \$\endgroup\$ – Lynob Aug 2 '17 at 10:18
  • \$\begingroup\$ 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? \$\endgroup\$ – magenulcus Aug 2 '17 at 18:11

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.