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\$
1
  • \$\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
    Commented Jul 29, 2017 at 20:56

1 Answer 1

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\$
4
  • \$\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
    Commented Aug 1, 2017 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
    Commented Aug 1, 2017 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
    Commented Aug 2, 2017 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
    Commented Aug 2, 2017 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .