Source generator for Godot 4 .NET version. It adds an OnReady attribute which automatically implements a property or method which calls GetNode or GetNodeOrNull.
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>
Add the Onready attribute to any partial property or method in a class deriving from Godot.Node.
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");
}
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");
}
}
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.