C# exports¶
Introduction to exports¶
In Godot, class members can be exported. This means their value gets saved along
with the resource (such as the scene) they're
attached to. They will also be available for editing in the property editor.
Exporting is done by using the [Export]
attribute.
using Godot;
public partial class ExportExample : Node3D
{
[Export]
private int Number = 5;
}
In that example the value 5
will be saved, and after building the current project
it will be visible in the property editor.
One of the fundamental benefits of exporting member variables is to have them visible and editable in the editor. This way, artists and game designers can modify values that later influence how the program runs. For this, a special export syntax is provided.
Exporting can only be done with Variant-compatible types.
Note
Exporting properties can also be done in GDScript, for information on that see GDScript exports.
Basic use¶
Exporting can work with fields and properties.
[Export]
private int _number;
[Export]
public int Number { get; set; }
Exported members can specify a default value; otherwise, the default value of the type is used instead.
[Export]
private int _number; // Defaults to '0'
[Export]
private string _text; // Defaults to 'null' because it's a reference type
[Export]
private string _greeting = "Hello World"; // Exported field specifies a default value
[Export]
public string Greeting { get; set; } = "Hello World"; // Exported property specifies a default value
// This property uses `_greeting` as its backing field, so the default value
// will be the default value of the `_greeting` field.
[Export]
public string GreetingWithBackingField
{
get => _greeting;
set => _greeting = value;
}
Resources and nodes can be exported.
[Export]
public Resource Resource { get; set; }
[Export]
public Node Node { get; set; }
Grouping exports¶
It is possible to group your exported properties inside the Inspector with the [ExportGroup]
attribute. Every exported property after this attribute will be added to the group. Start a new
group or use [ExportGroup("")]
to break out.
[ExportGroup("My Properties")]
[Export]
public int Number { get; set; } = 3;
The second argument of the attribute can be used to only group properties with the specified prefix.
Groups cannot be nested, use [ExportSubgroup]
to create subgroups within a group.
[ExportSubgroup("Extra Properties")]
[Export]
public string Text { get; set; } = "";
[Export]
public bool Flag { get; set; } = false;
You can also change the name of your main category, or create additional categories in the property
list with the [ExportCategory]
attribute.
[ExportCategory("Main Category")]
[Export]
public int Number { get; set; } = 3;
[Export]
public string Text { get; set; } = "";
[ExportCategory("Extra Category")]
[Export]
private bool Flag { get; set; } = false;
Note
The list of properties is organized based on the class inheritance, and new categories break that expectation. Use them carefully, especially when creating projects for public use.
Strings as paths¶
Property hints can be used to export strings as paths
String as a path to a file.
[Export(PropertyHint.File)]
public string GameFile { get; set; }
String as a path to a directory.
[Export(PropertyHint.Dir)]
public string GameDirectory { get; set; }
String as a path to a file, custom filter provided as hint.
[Export(PropertyHint.File, "*.txt,")]
public string GameFile { get; set; }
Using paths in the global filesystem is also possible, but only in scripts in tool mode.
String as a path to a PNG file in the global filesystem.
[Export(PropertyHint.GlobalFile, "*.png")]
public string ToolImage { get; set; }
String as a path to a directory in the global filesystem.
[Export(PropertyHint.GlobalDir)]
public string ToolDir