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:
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”