Godot Version
4.2.2
Question
I was building a 2D strategy game in Unity. Now I’m remaking it in Godot cuz Unity…yeah. Sorry if this is really simple, but I’m very new to Godot and it’s a weird new direction after using Unity for years.
I’m trying to build a way to select game units by clicking on them. I have a node that handles showing info about the selected unit and giving it orders. I need that Orders node to be able to grab whatever unit I click on and store it as a variable or in an array.
In Unity, I did this by raycasting from the camera to the point I clicked on. The raycast returned the unit, and my Orders object could display its info and then tell it what to do when I built the order I wanted to give it.
Every tutorial or post I’ve found says to use signals to do this in Godot. From what I understand, I need to add an Area2D with a collision child node, then wire it up to send out a “clicked” (or whatever) signal when the mouse clicks on the Area2D. My orders node can listen for that signal and then grab the unit from there.
All of the tutorials I find seem to want to add a bit of code to the Area2D/collider that listens for mouse clicks by checking for an event, then checking if it’s a mouse click, then checking if the mouse was over the collider. This seems really inefficient. If I have 200 units on the screen, I don’t want every single one of them running this test every time the player clicks. When I click on one unit, I want that unit’s node sent to my Orders node without a hundred scripts firing.
I do NOT want the orders handled by the unit itself. Eg, click on the unit, it pops up a box and you can give it orders with all the code handled right there in the unit’s node. The orders are complex and I have a dedicated node for packing them up all nice and delivering them to the unit. I could probably rework that, but tbh I really don’t want to.
Is there a simple way to click on one sprite among many other sprites and send that node to a script in a totally separate node without a bunch of checking for mouse click events across every sprite?