GodotSharp.SourceGenerators
C# Source Generators for use with the Godot Game Engine
Notes
- On GitHub, items marked as [NEW] are only available in pre-release (ie, well tested, but subject to subtle API changes - a good opportunity to test against your own use cases!)
- Version 2.7 introduces an ever so slight [BREAKING CHANGE] in that scope strings have been replaced with a more definitive enum set.
- Version 2.7 introduces an ever so slight [BREAKING CHANGE] in that the Autoload class must now be explicitly decorated instead of implicitly generated.
- Version 2.7 introduces an ever so slight [BREAKING CHANGE] in that identifiers comprised of unicode characters no longer need to be prefixed with
_ and other invalid characters are now removed instead of being replaced with _.
Features
SceneTree class attribute:
- Provides strongly typed access to the scene hierarchy (via
_ operator)
- Generates direct access to uniquely named nodes via class properties
- ISceneTree interface for use with generics (GD4 only)
- Provides static access to TscnFilePath
- IInstantiable interface for use with generics (GD4 only)
- Provides static Instantiate method
- Custom default scope for uniquely named node properties
- Specific properties can be overridden with a partial property (GD4 only)
AnimNames class attribute (GD4 only):
- Provides strongly typed access to animation names defined in .tres and .tscn files
AudioBus class attribute (GD4 only):
- Provides strongly typed access to audio bus names and ids
AutoEnum enum/class attribute (GD4 only):
- (enum) Generates efficient Str/Parse for enums
- (class) Generates enum for static data classes (for editor/network use)
Autoload class attribute:
- Provide strongly typed access to autoload nodes defined in godot.project
CodeComments class attribute:
- Provides a nested static class to access property comments from code (useful for in-game tooltips, etc)
GlobalGroups class attribute (GD4 only):
- Provides strongly typed access to global groups defined in godot.project
GodotOverride method attribute:
- Allows use of On*, instead of virtual _* overrides
- (Requires partial method declaration for use with Godot 4)
InputMap class attribute:
- Provides strongly typed access to input actions defined in godot.project
- Attribute option to replace StringName with your own custom object/handler
Instantiable class attribute (GD4 only):
- Generates configurable static method(s) to instantiate scene
LayerNames class attribute:
- Provide strongly typed access to layer names defined in godot.project
Notify property attribute:
- Generates boiler plate code, triggering only when values differ
- (Automagically triggers nested changes for Resource and Resource[])
OnImport method attribute (GD4 only):
- Generates default plugin overrides and options to make plugin class cleaner (inherit from OnImportEditorPlugin)
- (This will be removed in next major release)
OnInstantiate method attribute:
- Generates a static Instantiate method with matching args that calls attributed method as part of the instantiation process
- (Also generates a protected constructor to ensure proper initialisation - can be deactivated via attribute)
- (This will be removed in favour of
Instantiable in next major release)
ResourceTree class attribute (GD4 only):
- Provides strongly typed access to the resource hierarchy
- Generators for
Rpc methods (GD4 only):
- Provides strongly typed access to Rpc and RpcId methods
Shader class attribute (GD4 only):
- Provides strongly typed access to shader uniforms (or visual shader parameters)
ShaderGlobals class attribute (GD4 only):
- Provides strongly typed access to global shader uniforms defined in godot.project
Singleton class attribute (GD4 only):
- Provides single instance access to data or scene objects
TR class attribute (GD4 only):
- Provides strongly typed access to translation locales and keys (as defined in csv)
- Includes base classes/helpers to create project specific source generators
- Version 1.x supports Godot 3 only
- Version 2.x supports Godot 3 & 4
- Version 3.x will support Godot 4 only
Table of Contents
Installation
Install via NuGet
Documentation
SceneTree
- Class attribute
- Provides strongly typed access to the scene hierarchy (via
_ operator)
- Generates direct access class properties for uniquely named nodes
- (GD4 only) Generates an interface for static tscn retrieval (ISceneTree.TscnFilePath)
- (GD4 only) Generates an interface for static instantiation (IInstantiable.Instantiate)
- Note that nodes are cached on first access to avoid interop overhead
- Advanced options available as attribute arguments:
- tscnRelativeToClassPath: (default null) Specify path to tscn relative to current class
- traverseInstancedScenes: (default false) Include instanced scenes in the generated hierarchy
- root: (default _) Provide alternative to
_ operator (eg, to allow use of C# discard variable)
Examples:
// Attach a C# script on the root node of the scene with the same name
// [SceneTree] will generate the members as the scene hierarchy and TscnFilePath property
[SceneTree]
//[SceneTree(root: "ME")] // Use this for alternative to `_`
//[SceneTree("my_scene.tscn")] // Use this if tscn has different name
//[SceneTree("../Scenes/MyScene.tscn")] // Use relative path if tscn located elsewhere
//[SceneTree(traverseInstancedScenes: true)] // Use this to include instanced scenes in current hierarchy
//[SceneTree(uqScope: Scope.Protected)] // Use this to specify default scope of uniquely named nodes (default: 'Public')
public partial class MyScene : Node
{
// Default scope of uniquely named nodes can be overridden using partial properties [NEW - GD4 only]
private partial MyNodeType MyNodeWithUniqueName { get; }
public override void _Ready()
{
// You can access the node via '_' object
GD.Print(_.Node1.Node11.Node12.Node121);
GD.Print(_.Node4.Node41.Node412);
// You can also directly access nodes marked as having a unique name in the editor
GD.Print(MyNodeWithUniqueName);
GD.Print(_.Path.To.MyNodeWithUniqueName); // Long equivalent
// Only leaf nodes are Godot types (call .Get() on branch nodes)
// Lets say you have _.Node1.Node2, observe the following code
GD.Print(_.Node1.Name); // invalid
GD.Print(_.Node1.Get().Name); // valid
Node node1 = _.Node1; // implicit conversion also possible!
GD.Print(node1.Name); // valid
GD.Print(_.Node1.Node2.Name); // valid
}
}
// TscnFilePath usage:
public void NextScene()
=> GetTree().ChangeSceneToFile(MyScene.TscnFilePath);
ISceneTree (GD4 only)
- Generated for any class decorated with [SceneTree]
namespace Godot;
public partial interface ISceneTree
{
static abstract string TscnFilePath { get; }
}
Usage:
public void NextScene<T>() where T : ISceneTree
=> GetTree().ChangeSceneToFile(T.TscnFilePath);
IInstantiable (GD4 only)
- Provides a default Instantiate method that uses TscnFilePath
- Both non-generic and generic versions are available
- A default Instantiator class is also available
public partial interface IInstantiable
{
static T Instantiate<T>() where T : Node, ISceneTree
=> GD.Load<PackedScene>(T.TscnFilePath).Instantiate<T>();
}
public partial interface IInstantiable<T> where T : Node, IInstantiable<T>, ISceneTree
{
static T Instantiate() => GD.Load<PackedScene>(T.TscnFilePath).Instantiate<T>();
}
public static partial class Instantiator
{
public static T Instantiate<T>() where T : Node, ISceneTree
=> GD.Load<PackedScene>(T.TscnFilePath).Instantiate<T>();
}
Usage:
[SceneTree]
public partial class Scene1 : Node;
[SceneTree]
public partial class Scene2 : Node, IInstantiable;
[SceneTree]
public partial class Scene3 : Node, IInstantiable<Scene3>;
*****
// Instantiator works for all ISceneTree types
var scene1 = Instantiator.Instantiate<Scene1>();
var scene2 = Instantiator.Instantiate<Scene2>();
var scene3 = Instantiator.Instantiate<Scene3>();
// The non-generic interface can also instantiate any ISceneTree type (but why would you want to)
var scene1 = IInstantiable.Instantiate<Scene1>();
var scene2 = IInstantiable.Instantiate<Scene2>();
var scene3 = IInstantiable.Instantiate<Scene3>();
// The generic interface can only instantiate it's own ISceneTree type (but why would you want to)
var scene3 = IInstantiable<Scene3>.Instantiate();
// Use generics to instantiate specific types
static T Instantiate<T>() where T : Node, ISceneTree, IInstantiable
=> IInstantiable.Instantiate<T>(); // or Instantiator.Instantiate<T>();
var scene2 = Instantiate<Scene2>();
OR
static T Instantiate<T>() where T : Node, ISceneTree, IInstantiable<T>
=> IInstantiable<T>.Instantiate(); // or Instantiator.Instantiate<T>();
var scene3 = Instantiate<Scene3>();
AnimNames
- Class attribute
- Provides strongly typed access to animation names defined in .tres and .tscn files
- Supports AnimationLibrary (AnimationPlayer) and SpriteFrames (AnimatedSprite) animation names
- Supports animations saved to tres or embedded in tscn
- Supports flat list of names for static classes
- Advanced options available as attribute arguments:
- path: (default null) Provide path to tscn/tres if not same folder/same name
Examples:
[SceneTree, AnimNames]
public partial class MyScene : Node;
[AnimNames]
//[AnimNames("path")] // (optional path to tscn/tres)
public static partial class MyAnims;
Generates:
partial class MyScene
{
public static class AnimName
{
public static readonly StringName Anim1 = "Anim1";
public static readonly StringName Anim2 = "Anim2";
}
}
public static class MyAnims
{
public static readonly StringName Anim1 = "Anim1";
public static readonly StringName Anim2 = "Anim2";
}
AudioBus
- Class attribute
- Provides strongly typed access to audio bus names and ids
- Scrapes data from res://default_bus_layout.tres (or other provided path)
- Advanced options available as attribute arguments:
- source: (default 'default_bus_layout') relative or absolute resource path
Examples:
[AudioBus]
//[AudioBus("Resources/custom_bus_layout")] // Relative to current C# file or absolute path from project root (res:// prefix and .tres extension optional)
public static partial class AudioBus;
Generates:
static partial class AudioBus
{
public const int MasterId = 0;
public const int MusicId = 1;
public const int FxId = 2;
public static readonly StringName Master = "Master";
public static readonly StringName Music = "Music";
public static readonly StringName Fx = "FX";
}
AutoEnum
- Class/Enum attribute
- When decorating enum, generates Str/Parse extensions
- When decorating class, generates enum & conversions for static data
- Can be used to select enum in editor and translate to data in script or serialise across network
Examples:
// Decorated Enum
[AutoEnum]
public enum MapType
{
City,
Corridor,
Apartment,
}
// Decorated Class
[AutoEnum]
public partial class MapData
{
private MapData() { }
public static readonly MapData Outside = new(/* Init data */);
public static readonly MapData Corridor = new(/* Init data */);
public static readonly MapData Apartment = new(/* Init data */);
// Add data fields here
}
Generates: