Skip to content

Shaka-Rule/Shaka.Godot.OnReady

Repository files navigation

Shaka.Godot.OnReady

Source generator for Godot 4 .NET version. It adds an OnReady attribute which automatically implements a property or method which calls GetNode or GetNodeOrNull.

Setup

Add the Shaka.Godot.OnReady package through nuget package manager.

Or add this to your csproj file:

<ItemGroup>
    <PackageReference Include="Shaka.Godot.OnReady" Version="1.0.0"/>
</ItemGroup>

Usage

Add the Onready attribute to any partial property or method in a class deriving from Godot.Node.

Property(Only available from .NET9):

public partial class Player : CharacterBody2D
{
    [OnReady("%Sprite2D")]
    private partial Sprite2D Sprite { get; }
}

This will generate:

public partial class Player
{
    private Sprite2D? _sprite;
    private Sprite2D Sprite => _sprite ??= GetNode("%Sprite2D");
}

Method:

public partial class Player : CharacterBody2D
{
    [OnReady("%Sprite2D")]
    private partial Sprite2D Sprite();
}

This will generate:

public partial class Player
{
    private Sprite2D? _sprite;
    private Sprite2D Sprite
    {
        return _sprite ??= GetNode("%Sprite2D");
    }
}

Nullable

When using a nullable type, the generator will instead generate this:

public partial class Player
{
    private Sprite2D? _sprite;
    private Sprite2D? Sprite => _sprite ??= GetNodeOrNull("%Sprite2D");
}

This way your code will not crash when getnode fails and you can handle nulls yourself.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Languages