Getting children of Root note seem to return only root node

GODOT version 4.2.1 (Mono)

Main code
using Godot;
using System;

public partial class nearbycheck : CsgBox3D
{
    public float MaximumDistance = 10.0f;

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        base._Process(delta);
        // Personal note, C# doesn't use Foreachs like Java does, but instead of "for" it requires "foreach"
        foreach (var Object in GetTree().Root.GetChildren(true))
        {
            // You can't use .Equals or == with Godot's classes.
            if (Object is CharacterBody3D LocalCharacter)
            {
                // Calculating the distance between each object and the pole, not most optimal but still something ay?
                var Distance = LocalCharacter.GlobalTransform.Origin - GlobalTransform.Origin;
                // Length() apparently is a getter, so I can't just use it 
                if (Distance.Length() < MaximumDistance)
                {
                    GD.Print("A player is nearby...");
                }
                else
                {
                    GD.Print("No one nearby...");
                }
            }
            else
            {
                GD.Print("No object of class 'CharacterBody3D' was found...");
            }
        }
    }
}

Debugging this code while playing my project, I noticed it kept printing “No object of class…”
Tried to write code to further analyse the situation, and I realised that somewhy the code only sees the root “Node3D” node.
Does anybody happen to know why does this happen and a possible fix?

Code used to debug:
public override void _Process(double delta)
{
    base._Process(delta);
    foreach (var Object in GetTree().Root.GetChildren())
    {
        GD.Print("NAME: " + Object.Name + "CLASS: " + Object.GetType());
    }
}

This is the hierarchy I find myself within:
image

This is the output:

NAME: Node3DCLASS: Godot.Node3D
NAME: Node3DCLASS: Godot.Node3D
NAME: Node3DCLASS: Godot.Node3D
NAME: Node3DCLASS: Godot.Node3D
NAME: Node3DCLASS: Godot.Node3D

Side note: Main code is inside “CSGBox3D”

Root can have only one child
try

	public override void _Ready()
	{
		foreach (var Object in GetTree().Root.GetChild(0).GetChildren())
		{
			GD.Print($"NAME: {Object.Name}, CLASS: {Object.GetType()}");
		}
	}
1 Like

Returns

NAME: Node2D, CLASS: Godot.Node2D
NAME: PlayerCharacter, CLASS: PlayerCharacter
NAME: WorldEnvironment, CLASS: Godot.WorldEnvironment
NAME: BoxesContainer, CLASS: Godot.Node3D
NAME: CSGBox3D, CLASS: nearbycheck

But it seems that it doesn’t see “PlayerCharacter” as a CharacterBody3D, know any reason onto why is that happening?

Edit: Yet it still works when implemented in the _Processed, thank you

your PlayerCharacter inherits from CharacterBody3D?

Yes, it indeed does, that’s why I was confused, but upon implementing your way of getting the descendants, I managed to get this result.
image
Which is exactly what I wanted

1 Like

if player don’t move in your tree and is only one
var player GetNodeOrNull<CharacterBody3D>("PlayerCharacter");
can be faster

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.