Best practice for accessing 'sibling' nodes

(Godot Version 4.3)

Hi, I have a quick question about node/scene architecture and accessing other nodes.

Within this scene, which is a in-world object (in this case a book), I currently have a node “ObjectHandler” which handles replacing the texture of a mesh when the object is interacted with (highlight when the crosshairs are over it, flashes red when hit, etc…)

I’m intending this ObjectHandler node to be used in multiple places and added to any interactable objects.

I am currently accessing all the mesh instances by using get_parent() and then searching for child nodes that are MeshInstance3D.

Is it fine in this scenario to search up the tree and then down, or should I be using a different method? Should I put all the mesh instances under a specifically named node and use that as a point to search from?

Any advice is very appreciated!

get_parent() sounds fine, iterate through children recursively while checking for the MeshInstance3D type. I’ve done it before but it’s rare, what’s the greater use case, big picture what is driving this solution?

Hi, thanks for the reply.

In it’s current form it’s just to visually show that a moused over object can be interacted with. I wanted something I can add to any scene to enable this behaviour.

This currently gets triggered from a raycast attached to the camera. A script checks for an ObjectHandler node and then calls a function within the ObjectHandler script to add an override texture. Another function then removes the override texture when the raycast stops hitting that object’s collider.

Generally I’m just wondering if there’s a better way to access/search for other nodes just within the scene the current node is in.

This is it; there are groups but those operate over everything in the group, not just descendants.

That is the exact same scenario I have used such code. Highlighting 3D objects.

1 Like