Hey everybody, quick question:
I am developing an RTS game and the time has come to implement units' "attack" order, which means I need to check which unit was clicked.
Beforehand, I used unhandled_input queue for handling movement commands.
To select units, I am currently iterating over all the selectable units and looking for the one with closest distance from the click -> this will underperform when attacking, as there are several times more units that are "attackable" than "selectable".
I noticed that there is an "input_event" signal for the bodies which seems to be what I am looking for, but unfortunately the events are handled by the unhandled_input function first (responsible unit selection and movement commands).
Any ideas how to do it better than iterating through all attackable units?
Thanks for responses 🙂
Maks

  • Yes, you can create an Area2D and use get overlapping bodies.

    It can also be easier if you create it at the beginning, and then let if follow the mouse (but it is hidden and or disabled) and only enable it briefly on click.

    Note that the physics engine can take at least a frame or two to process, so if you show/create it on click, you may need to wait until the next frame to check it.

I am actually thinking about dynamically creating Area2D and using get_overlapping_bodies for both unit selection and attacking.

  • Maks replied to this.

    Yes, you can create an Area2D and use get overlapping bodies.

    It can also be easier if you create it at the beginning, and then let if follow the mouse (but it is hidden and or disabled) and only enable it briefly on click.

    Note that the physics engine can take at least a frame or two to process, so if you show/create it on click, you may need to wait until the next frame to check it.

    Maks Yes that is actually what is happening, I need to implement selecting a unit with one frame delay.