Skip to content

Latest commit

 

History

History
88 lines (71 loc) · 2.8 KB

README.md

File metadata and controls

88 lines (71 loc) · 2.8 KB

GetNodeAutoProp

Incremental C# source code generator for Godot's GetNode fields, inspired by Godot Proposal #2425.

Quick start

using GetNodeAutoProp;

partial class BouncingEnemy : Node2D
{
    [Export]
    public float Speed { get; private set; } = 50.0f;

    [GetNode("./EnemySprite")]
    private AnimatedSprite2D? _sprite;
    [GetNode]
    private RayCast2D? _rayCastLeft;
    [GetNode]
    private RayCast2D? _rayCastRight;

    public override void _Process(double delta)
    {
        if (RayCastLeft.IsColliding())
        {
            Sprite.FlipH = false;
        }
        else if (RayCastRight.IsColliding())
        {
            Sprite.FlipH = true;
        }

        int direction = Sprite.FlipH ? -1 : 1;
        Position += new Vector2((float)(delta * Speed * direction), 0);
    }
}

Usage

  1. Include GetNodeAutoProp namespace
  2. Declare your dependencies as nullable fields with [GetNodeAttribute("./path/to/node")]
  3. Upon compilation, each fields will generate a property with same name, but capitalized and stripped of underscores
  4. The path can be omitted, defaults to the same name as generated property

The example code from quick start will generate the following file:

partial class BouncingEnemy
{
    private AnimatedSprite2D Sprite => _sprite ??= GetNode<AnimatedSprite2D>("./EnemySprite");
    private RayCast2D RayCastLeft => _rayCastLeft ??= GetNode<RayCast2D>("RayCastLeft");
    private RayCast2D RayCastRight => _rayCastRight ??= GetNode<RayCast2D>("RayCastRight");
}

Installation

  1. Download the Generator folder and put it inside your project, e.g. in plugins
  2. Open your game's .csproj file, in Godot requires changing "All recognized" to "All" in "Open file" dialog
  3. Change the <TargetFramework> to net8.0 or higher
  4. Add the following at the end of <Project> section:
  <ItemGroup>
    <ProjectReference
      Include="plugins/Generator/Generator.csproj"
      OutputItemType="Analyzer"
      ReferenceOutputAssembly="true"
    />
    <Compile Remove="plugins/Generator/**/*.cs" />
  </ItemGroup>
1. Recompile the project

FAQ

Q: How can I see the generated code?
A: Add the following to the <PropertyGroup> section of your game's .csproj file and recompile the project:

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>

Q: Why my IDE does not recognize generated properties?
A: Code completion engines don't always play nicely with generated code. You may need to reload the completion engine or restart your IDE completely. This may be required just once per project, after a new file is added, or every time you add new [GetNode] field.

  • For (Neo)vim YCM use :YcmRestartServer