### C# OnInstantiate Attribute for Scene Instantiation Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md Illustrates the `OnInstantiate` method attribute, which generates a static `Instantiate` method for a scene, calling the attributed method as part of the instantiation process. Explains the generated simplified code and provides a usage example for adding the scene to the world. ```C# public partial class MyScene : Node { [OnInstantiate] private void Initialise(string myArg1, int myArg2) => GD.PrintS("Init", myArg1, myArg2); } ``` ```C# private static PackedScene __scene__; private static PackedScene __Scene__ => __scene__ ??= GD.Load("res://Path/To/MyScene.tscn"); public static MyScene Instantiate(string myArg1, int myArg2) { var scene = __Scene__.Instantiate(); scene.Initialise(myArg1, myArg2); return scene; } private Test3Arg() {} ``` ```C# // ... in some class private void AddSceneToWorld() { var myScene = MyScene.Instantiate("str", 3); MyWorld.AddChild(myScene); } ``` -------------------------------- ### OnInstantiate Attribute for Custom Instantiation Logic Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `OnInstantiate` method attribute generates a static `Instantiate` method with matching arguments that calls the attributed method as part of the instantiation process. It also generates a protected constructor to ensure proper initialization, which can be deactivated via an attribute argument, streamlining object creation with custom setup. ```APIDOC `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) ``` -------------------------------- ### C# CodeComments Attribute for Property Comment Access Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md Explains the `CodeComments` class attribute which generates a nested static class to access property comments from code. Provides an example of how to use the attribute on a C# class and retrieve comments for an exported property, highlighting its limitation to properties. ```C# [CodeComments] public partial class CodeCommentsTest : Node { // This a comment for Value1 // [CodeComments] only works with Property [Export] public float Value1 { get; set; } // Value 2 is a field so no comment [Export] public float value2; public override _Ready() { GD.Print(GetComment(nameof(Value1))); // output: "This a comment for Value1\n[CodeComments] only works with Property" GD.Print(GetComment(nameof(value2))); // output: "" (No output for fields, but could be added if needed) } } ``` -------------------------------- ### Godot Autoload Configuration and Generated C# Class Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md Demonstrates how to configure autoloads in `project.godot` and define renames using the `AutoloadRename` attribute in C#. Shows the corresponding C# class generated by the source generator, providing static accessors for autoloaded nodes and scenes. ```GDScript [autoload] gd_utils="*res://addons/handy_utils/gd_utils.gd" cs_utils="*res://addons/silly_sausage/MyUtils.cs" DebugMenu="*res://addons/debug_menu/debug_menu.tscn" ``` ```C# namespace Godot; [AutoloadRename("UtilsGD", "gd_utils")] [AutoloadRename("UtilsCS", "cs_utils")] static partial class Autoload; ``` ```C# namespace Godot; static partial class Autoload { private static Node root = (Engine.GetMainLoop() as SceneTree)?.Root; /// Autoload: gd_utils public static Node UtilsGD => field ??= root?.GetNodeOrNull("gd_utils"); /// Autoload: cs_utils public static MyUtils UtilsCS => field ??= root?.GetNodeOrNull("cs_utils"); /// Autoload: DebugMenu public static CanvasLayer DebugMenu => field ??= root?.GetNodeOrNull("DebugMenu"); } ``` -------------------------------- ### InputMap Attribute for Strongly Typed Input Actions Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `InputMap` class attribute provides strongly typed access to input actions defined within the `godot.project` file. This eliminates the need for string-based lookups, improving code safety and readability when interacting with Godot's input system. ```APIDOC `InputMap` class attribute: Provides strongly typed access to input actions defined in godot.project ``` -------------------------------- ### APIDOC: OnImport Method Attribute Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md Documents the `OnImport` method attribute (Godot 4 only) which generates default plugin overrides and options, simplifying plugin class creation. It includes base classes/helpers for creating project-specific source generators and is noted as potentially less useful for general use. ```APIDOC `OnImport` * Method attribute (GD4 only) * Generates default plugin overrides and options to make plugin class cleaner (inherit from OnImportEditorPlugin) * Includes base classes/helpers to create project specific source generators * (Not that useful unless writing lots of plugins - might remove in v3) ``` -------------------------------- ### InputMap Attribute for Strongly Typed Godot Input Actions Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `InputMap` class attribute provides strongly typed access to input actions defined in the `godot.project` file. It simplifies accessing actions compared to manual `StringName` declarations. ```C# [InputMap] public static partial class MyInput; ``` ```C# // (static optional) // (string rather than StringName for Godot 3) // (does not provide access to built-in actions) partial static class MyInput { public static readonly StringName MoveLeft = "move_left"; public static readonly StringName MoveRight = "move_right"; public static readonly StringName MoveUp = "move_up"; public static readonly StringName MoveDown = "move_down"; } ``` -------------------------------- ### Autoload/AutoloadRename Attributes for Strongly Typed Autoload Nodes Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `Autoload` and `AutoloadRename` class attributes provide strongly typed access to autoload nodes defined in the `godot.project` file. This allows for direct, type-safe referencing of global singletons, improving code maintainability and reducing runtime errors. ```APIDOC `Autoload`/`AutoloadRename` class attribute: Provide strongly typed access to autoload nodes defined in godot.project ``` -------------------------------- ### CodeComments Attribute for Accessing Property Comments Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `CodeComments` class attribute generates a nested static class that provides programmatic access to property comments from code. This is particularly useful for scenarios like generating in-game tooltips or documentation directly from source code comments. ```APIDOC `CodeComments` class attribute: Provides a nested static class to access property comments from code (useful for in-game tooltips, etc) ``` -------------------------------- ### LayerNames Attribute for Strongly Typed Layer Access Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `LayerNames` class attribute provides strongly typed access to layer names defined in the `godot.project` file. This simplifies working with Godot's physics and rendering layers by replacing magic strings with compile-time checked properties. ```APIDOC `LayerNames` class attribute: Provide strongly typed access to layer names defined in godot.project ``` -------------------------------- ### GodotOverride Attribute for C# Method Overrides Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `GodotOverride` attribute allows C# methods to use `On*` naming conventions instead of `_*` for Godot virtual overrides. It supports advanced options like `replace` to skip base call generation. For Godot 4.0, partial method declarations are required for the `_*` methods. ```C# public partial class MyNode : Node2D { [GodotOverride] protected virtual void OnReady() => GD.Print("Ready"); [GodotOverride(replace: true)] private void OnProcess(double delta) => GD.Print("Processing"); // Requires partial method declaration for use with Godot 4.0 public override partial void _Ready(); public override partial void _Process(double delta); } ``` ```C# public override void _Ready() { base._Ready(); OnReady(); } public override _Process(double delta) => OnProcess(delta); ``` -------------------------------- ### LayerNames Attribute for Strongly Typed Godot Layer Access Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `LayerNames` class attribute provides strongly typed access to layer names defined in the `godot.project` file. It generates constants for 2D and 3D render, physics, and navigation layers, including mask values. ```C# [LayerNames] public static partial class MyLayers; ``` ```C# // (static optional) public static partial class MyLayers { public static class Render2D { public const int MyLayer1 = 0; // Yes, layers start at 1 in editor, but 0 in code public const int MyLayer2 = 1; public const int MyLayer7 = 6; public const int _11reyaLyM = 10; // Yes, we will append an underscore if required... public static class Mask { public const uint MyLayer1 = 1 << 0; public const uint MyLayer2 = 1 << 1; public const uint MyLayer7 = 1 << 6; public const uint _11reyaLyM = 1 << 10; } } public static class Render3D { public const int MyLayer1 = 0; public static class Mask { public const uint MyLayer1 = 1 << 0; } } // Also for Physics2D, Physics3D, Navigation2D, Navigation3D, Avoidance, etc... } ``` -------------------------------- ### OnImport Attribute for Editor Plugin Overrides (Godot 4) Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `OnImport` method attribute (Godot 4 only) generates default plugin overrides and options, allowing plugin classes to inherit from `OnImportEditorPlugin`. This simplifies the creation of cleaner and more structured editor plugins by handling common boilerplate. ```APIDOC `OnImport` method attribute (GD4 only): Generates default plugin overrides and options to make plugin class cleaner (inherit from OnImportEditorPlugin) ``` -------------------------------- ### GodotOverride Attribute for Custom Method Overrides Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `GodotOverride` method attribute allows C# developers to use `On*` naming conventions instead of `virtual _*` for Godot method overrides. This requires a `partial` method declaration when used with Godot 4, simplifying the override syntax. ```APIDOC `GodotOverride` method attribute: Allows use of On*, instead of virtual _* overrides (Requires partial method declaration for use with Godot 4) ``` -------------------------------- ### Notify Attribute for C# Property Change Events Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `Notify` attribute generates public `ValueChanging` and `ValueChanged` events for properties, automatically triggering nested changes for `Resource` and `Resource[]` types. Events are only raised if the value changes, and initial values can be set without triggering updates. Partial properties are supported. ```C# public partial class NotifyTest : Node { [Notify] public partial int Value { get; set; } // [NEW] Partial properties now supported [Notify] public float Value1 { get => _value1.Get(); set => _value1.Set(value); // You can also pass onchanged event handler here } public override void _Ready() { Value1Changing += () => GD.Print("Value1Changing raised before value is changed"); Value1Changed += () => GD.Print("Value1Changed raised after value is changed"); // You can also subscribe to private events if needed // _value1.Changing += OnValue1Changing; // _value1.Changed += OnValue1Changed; // This might be useful... erm... if you need to clear the public listeners for some reason...? Value1 = 1; // Raise Value1Changing and Value1Changed Value1 = 2; // Raise Value1Changing and Value1Changed Value1 = 2; // No event is raised since value is the same } public NotifyTest() => InitValue1(7); // Set initial value without triggering events (optional) } ``` -------------------------------- ### Using SceneTree Attribute for Strongly Typed Node Access in Godot C# Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `SceneTree` class attribute generates C# properties for uniquely named nodes within a Godot scene, providing strongly typed access to the scene hierarchy via the `_` operator. It also generates a static `TscnFilePath` property for scene resource access. Nodes are cached for performance, and the attribute supports advanced options like specifying relative tscn paths, traversing instanced scenes, and customizing the root access operator. ```C# // Attach a C# script on the root node of the scene with the same name. // [SceneTree] will generate the members as the scene hierarchy. [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 public partial class MyScene : Node2D { 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 } } ... // (elsewhere) public void NextScene() => GetTree().ChangeSceneToFile(MyScene.TscnFilePath); ``` -------------------------------- ### Notify Attribute for Property Change Notifications Source: https://github.com/cat-lips/godotsharp.sourcegenerators/blob/main/README.md The `Notify` property attribute generates boilerplate code to trigger property change notifications only when a value differs from its previous state. It automatically handles nested changes for `Resource` and `Resource[]` types, reducing manual implementation for reactive properties. ```APIDOC `Notify` property attribute: Generates boiler plate code, triggering only when values differ (Automagically triggers nested changes for Resource and Resource[]) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.