Skip to content

Instantly share code, notes, and snippets.

@mrdaemon
Last active September 18, 2023 03:22
Show Gist options
  • Save mrdaemon/9f8f5b80eb12942c15c063d47f30c479 to your computer and use it in GitHub Desktop.
Save mrdaemon/9f8f5b80eb12942c15c063d47f30c479 to your computer and use it in GitHub Desktop.
A Godot 4 C# class for an in-editor gizmo that displays the entity name above them
/**
* A basic gizmo that sets a Label3D child to the name of its parent.
* Arguably useful for development.
*
* The Label3D component will only be shown inside the Editor viewport.
* At runtime, the object and its hierarchy is removed entirely on call
* to _Ready().
*
* Caveats:
* - Super basic, the inverted triangle arrow is a hardcoded unicode
* character on a separate line.
* - Looking at a downwards angle or rotating the parent makes it look weird.
* It is really not intended for runtime.
* - It does not update if you rename the parent after instantiating it in
* the editor. Reloading the scene or project does, however.
*/
using Godot;
using System;
[Tool]
public partial class NameGizmo : Marker3D
{
private Node3D _parent;
public static Label3D _label;
public override void _Ready()
{
// Delete ourselves if we're not in the editor
// The entire node can just be removed at runtime.
if (!Engine.IsEditorHint())
{
QueueFree();
return;
}
try
{
_parent = GetParent<Node3D>();
_label = GetNode<Label3D>("Label3D");
_label.Text = _parent.Name + "\n▼";
}
catch (Exception) when (ex is InvalidCastException ||
ex is TypeAccessException)
{
GD.PrintErr("NameGizmo: Not parented to a Node3D, not updating.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment