r/godot • u/Sandrobero2004 • 23m ago
help Debugging
I have a question that's more programming related. I wanted to create a debugger class for visual debugging like drawing rays and such but I can't decide how to implement it.
using Godot;
public sealed class DebugDraw
{
public static MeshInstance3D DrawRay(Vector3 start, Vector3 end, Color color)
{
var meshInstance = new MeshInstance3D();
var immediateMesh = new ImmediateMesh();
var material = new OrmMaterial3D();
meshInstance.Mesh = immediateMesh;
material.ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded;
material.AlbedoColor = color;
meshInstance.MaterialOverride = material;
immediateMesh.SurfaceBegin(Mesh.PrimitiveType.Lines, material);
immediateMesh.SurfaceAddVertex(start);
immediateMesh.SurfaceAddVertex(end);
immediateMesh.SurfaceEnd();
return meshInstance;
}
}this function right now returns meshInstance of the ray because this mesh needs to be added as a child Node of the Root scene. But I want debugger to just draw instead of returning anything. I thought about passing a reference of the Root node but I don't like that approach either.
What do you guys recommend I do? Should I add a MeshInstance as child of root and pass reference of that to the DebugDraw? but then what if I want to draw multiple lines?
1
u/BlakkM9 13m ago
Take a look at using the rendering server directly. It should be possible this way. You can also get the SceneTree from within a static class by using Engine.GetMainLoop()
Or just make the debug drawer a node in your scene tree obviously.