Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Globale C#-Klassen

Global classes (also known as named scripts) are types registered in Godot's editor so they can be used more conveniently. In GDScript, this is achieved using the class_name keyword at the top of a script. This page describes how to achieve the same effect in C#.

  • Globale Klassen werden in den Dialogen Node hinzufügen und Ressource erstellen angezeigt.

  • Wenn es sich bei einer exportierten Property um eine globale Klasse handelt, schränkt der Inspektor die Zuweisung ein und erlaubt nur Instanzen dieser globalen Klasse oder abgeleiteter Klassen.

Globale Klassen werden mit dem Attribut [GlobalClass] registriert.

using Godot;

[GlobalClass]
public partial class MyNode : Node
{
}

Warnung

The file name must match the class name in case-sensitive fashion. For example, a global class named "MyNode" must have a file name of MyNode.cs, not myNode.cs.

Der Typ MyNode wird als globale Klasse mit dem gleichen Namen wie der Name des Typs registriert.

../../../_images/globalclasses_addnode.webp

Das Select a Node-Fenster für die exportierte Property MyNode filtert die Liste der Nodes in der Szene entsprechend der Zuweisungsbeschränkung.

public partial class Main : Node
{
    [Export]
    public MyNode MyNode { get; set; }
}
../../../_images/globalclasses_exportednode.webp

Wenn ein benutzerdefinierter Typ nicht als globale Klasse registriert ist, ist die Zuordnung auf den Godot-Typ beschränkt, auf dem der benutzerdefinierte Typ basiert. Beispielsweise sind Inspektorzuweisungen an einen Export des Typs MySimpleSprite2D auf Sprite2D und abgeleitete Typen beschränkt.

public partial class MySimpleSprite2D : Sprite2D
{
}

In Kombination mit dem [GlobalClass]-Attribut erlaubt das [Icon]-Attribut die Angabe eines Pfades zu einem Icon, das angezeigt wird, wenn die Klasse im Editor dargestellt wird.

using Godot;

[GlobalClass, Icon("res://Stats/StatsIcon.svg")]
public partial class Stats : Resource
{
    [Export]
    public int Strength { get; set; }

    [Export]
    public int Defense { get; set; }

    [Export]
    public int Speed { get; set; }
}
../../../_images/globalclasses_createresource.webp

Die Klasse Stats ist eine benutzerdefinierte Ressource, die als globale Klasse registriert ist. Der Export von Propertys des Typs Stats erlaubt nur die Zuweisung von Instanzen dieses Ressourcentyps, und mit dem Inspektor können Sie Instanzen dieses Typs einfach erstellen und laden.

../../../_images/globalclasses_exportedproperty1.webp ../../../_images/globalclasses_exportedproperty2.webp

Warnung

The Godot editor will hide these custom classes with names that begin with the prefix "Editor" in the "Create New Node" or "Create New Scene" dialog windows. The classes are available for instantiation at runtime via their class names, but are automatically hidden by the editor windows along with the built-in editor nodes used by the Godot editor.