Skip to content

Add [BindSharedEnum] attribute to share namespace-level enums across GDExtension classes#47

Draft
MLAcookie wants to merge 1 commit into
godotengine:masterfrom
MLAcookie:master
Draft

Add [BindSharedEnum] attribute to share namespace-level enums across GDExtension classes#47
MLAcookie wants to merge 1 commit into
godotengine:masterfrom
MLAcookie:master

Conversation

@MLAcookie

@MLAcookie MLAcookie commented Jun 11, 2026

Copy link
Copy Markdown

Summary

This PR introduces [BindSharedEnum] — a new attribute that allows namespace-level enums to be shared across multiple GDExtension [GodotClass] classes. Before, enums registered via [BindEnum] must be nested inside a [GodotClass] because the source generator only scans class members, forcing duplication when multiple extension classes need the same enum.

Before (enum duplicated per class)

// Each class must declare and register its own copy of the same enum
[GodotClass]
public partial class ClassA : Node
{
    [BindEnum]
    public enum Team { Red, Green, Blue }
}

[GodotClass]
public partial class ClassB : Node
{
    [BindEnum]
    public enum Team { Red, Green, Blue }  // Duplicated!
}

After (enum declared once, shared)

// Declare once at namespace level
public enum Team { Red, Green, Blue }

[GodotClass]
[BindSharedEnum(typeof(Team))]
public partial class ClassA : Node { }

[GodotClass]
[BindSharedEnum(typeof(Team))]
public partial class ClassB : Node { }
// sharing multiple enums on a single class
[GodotClass]
[BindSharedEnum(typeof(Team))]
[BindSharedEnum(typeof(ItemFlags))]
[BindSharedEnum(typeof(Status))]
public partial class Player : Node2D { }

Compatibility

  • Existing [BindEnum] on nested enums continues to work unchanged.
  • [BindSharedEnum] only affects classes that explicitly use it.
  • No breaking changes to the public API surface of Godot.Bindings.

Note

I recognize that godot-dotnet may not currently be in a phase where convenience features like this are a priority, and the [BindEnum] pattern already covers the basic use case. If this doesn't align with the current roadmap, feel free to close without hesitation.

Either way, thank you for your time reviewing this.

…asses

Introduce [BindSharedEnum(typeof(T))] which allows multiple Godot
extension classes to register constants from the same namespace-level
enum type. The enum itself needs no [BindEnum] attribute.

- New BindSharedEnumAttribute (AllowMultiple, targets Class)
- ClassSpecCollector extracts enum type from typeof() argument
- ConstantSpecCollector.CollectShared() skips [BindEnum] check
@MLAcookie
MLAcookie marked this pull request as ready for review June 12, 2026 06:18
@MLAcookie
MLAcookie marked this pull request as draft June 12, 2026 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant