Godot Version
Godot Engine v4.2.2.stable.official [15073afe3]
Question
I am currently in the process of implementing a system for a StaticBody3D called “Voxel” that allows the MeshInstance3D attached to it to change color whenever it is being looked at by the player.
func _physics_process(delta: float):
# other stuff...
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
# ray cast stuff cut for brevity
var _result = _spaceState.intersect_ray(_query)
if _result != null && _result.size() > 0:
for key in _result.keys():
print("{k}: {r}".format({"k": key, "r": _result[key]}))
emit_signal("isLookingAt", _result["collider"])
I then have a Node3D that acts as a “container” for all of my StaticBody3Ds. I have attached a signal handler to this container for the “isLookingAt” signal.
func _on_is_looking_at(collider: Node):
print("joe is looking at {node}".format({"node": collider.name}))
for child in collider.get_children():
print("child: {child}".format({"child": child}))
if child.name == "VoxelMesh":
child.get_active_material(0).albedo_color = Color(0, 0, 0)
The following information gets printed to the console whenever I look at one Voxel:
:>
position: (-0.550182, 0, -4.911057)
normal: (0, 1, 0)
face_index: -1
collider_id: 31004296529
collider: Voxel1:<StaticBody3D#31004296529>
shape: 0
rid: RID(4681514352646)
joe is looking at Voxel1
child: VoxelCollision:<CollisionShape3D#31021073746>
child: VoxelMesh:<MeshInstance3D#31037850963>
However, whenever I look at the results in-game, all of the voxels will get colored, despite the emitter triggering for one object’s collision.
I suspect that this has to do with the fact that the children of the Voxel (aptly named “VoxelMesh” and “VoxelCollision”) are all named the same regardless of which Voxel it is. I am simply checking the name of the child node to see if it is the MeshInstance3D that I want to change.
My issue is wanting to reference a particular child node’s children without changing the properties of other similarly-named children in the group. I’m unsure as to why this is happening, because as far as I’m aware accessing the child Node only should be changing only that Node’s MeshInstance3D.
If it’s possible to get the child node by a particular unique ID, that might work, but I’m not sure how to accomplish this. Any help is greatly appreciated.
Thanks.