Hi everyone. It's tough coming up with an apt title for this one.

I have multiple scenes with a RigidBody2D as the main node, then a Sprite and a CollisionShape2D

In my main scene I have several of these instantiated. In the script for each of them, I use _on_NodeName_input_event to detect the mouse being pressed and held down then I have code to move it with the mouse cursor or rotate it if it's the right mouse button.

My question is, since all the code is the same for each one, is there a way to detect if the mouse has been pressed on any of them in the main scene's script instead and then apply the mouse dragging and or/or rotating from there rather than having the same code in every script for every object?

like on_childX_input_event(body):
if left mouse button down do this to childX etc.

Any help would be most appreciated as always.

    It's best to have the input code on the main script (like on the root node) that way you have more control. You can loop through all the objects in the _input() function (either on click or motion) and then call functions on the objects to do the actual calculation.

      cybereality is there a way to detect which one has been clicked though?

      Obviously I'm currently doing that in the script of the node itself. Can this be detected in the main scene's script somehow or should I be passing a signal from the node's script?

      Well it's easier and more flexible to have a local script (on each object). But it becomes more complex when you want to coordinate. Like when you click on one, you disable input on the others, and that sort of thing would be easier on a global script. But it depends on the game which is best.

      In terms of the clicking, you would still use the local script for the signal, but pass that to the global script function for handling. You can also do the clicking logic manually, but having an Area2D follow the mouse, and then check get_overlapping_bodies when the mouse clicks. There are lots of ways.

        cybereality Thank you cybereality 👍 That makes sense. I'll try implementing something later (I'm at my day job 🙄)

        Hope you're well and I appreciate the help!

        It's also more performance friendly to only have 1 script on a parent node that uses _process() with _process() disabled on the other nodes.

          Megalomaniak Nice tip, Megalomaniak. Does that mean I should use set_process(false) in the children or just omit the process function from the child script?

          You can just leave out the process function, though you do get a slight boost from disabling it (set_process(false) in _ready()).

          madbeef one thing you can try is to put a variable, like a bool, in the object's script to determine which one is active. Do something like:
          var _isActive = false
          and then have that bool flip whenever your mouse enters the area?
          This way you only "grab" whatever is in the mouse area.

          Thanks @cybereality and @SnapCracklins

          This is all new to me despite having programming experience in the past. It's a real rabbit hole and amazing to learn new methods. I've managed to try out using signals with a test Child Node and connecting to it in the main scene, passing the node name and the fact that the LMB has been pressed.

          Now I'm thinking I'll try creating a signal bus in an autoloaded singleton so all the children can send signals with that and the main scene can connect to it. And as cybereality mentioned about disabling the other children when one is clicked, I could add all the children to a group, then when one is selected, remove that one from the group, disable the group whilst I'm dragging the selected one (or whatever) then add it back to the group upon de-selecting it.

          That's my thinking anyway. There may well turn out to be a better method for that! But the more I learn, the better I get and the more I can help others on their own journey with Godot.