GodotHat.SourceGenerators
0.0.1-dev-2-gd85498b
See the version list below for details.
dotnet add package GodotHat.SourceGenerators --version 0.0.1-dev-2-gd85498b
NuGet\Install-Package GodotHat.SourceGenerators -Version 0.0.1-dev-2-gd85498b
<PackageReference Include="GodotHat.SourceGenerators" Version="0.0.1-dev-2-gd85498b" />
paket add GodotHat.SourceGenerators --version 0.0.1-dev-2-gd85498b
#r "nuget: GodotHat.SourceGenerators, 0.0.1-dev-2-gd85498b"
// Install GodotHat.SourceGenerators as a Cake Addin #addin nuget:?package=GodotHat.SourceGenerators&version=0.0.1-dev-2-gd85498b&prerelease // Install GodotHat.SourceGenerators as a Cake Tool #tool nuget:?package=GodotHat.SourceGenerators&version=0.0.1-dev-2-gd85498b&prerelease
godothat
Alternate C# source generators for godot.
Attributes
OnReady
Implements _Ready
for you. More interesting if you return an IDisposable
[OnReady]
private void DoReadyThings()
{
// do your OnReady things
}
That's mildly useful, mostly down to style, though not much reason to not just override _Ready yourself. But godothat will also perform automatic cleanup of IDisposables on tree exit:
[OnReady]
private IDisposable SubscribeToFoos() => FooManager.WhenFoosUpdated.Subscribe(this.FoosUpdated);
private void FoosUpdated(IEnumerable<Foo> foos) {
// Do things with the new Foos
}
You can have multiple [OnReady]
methods.
OnEnterTree / OnExitTree
Like [OnReady]
, but for EnterTree. [OnExitTree]
for other explicit cleanup options.
[OnEnterTree]
private void ConnectMultiplayerEvents() {
var multiplayer = GetTree().GetMultiplayer();
multiplayer.PeerConnected += this.OnNetworkPeerConnected;
multiplayer.PeerDisconnected += this.OnNetworkPeerDisconnected;
}
[OnExitTree]
private void CleanupMultiplayerEvents() {
var multiplayer = GetTree().GetMultiplayer();
multiplayer.PeerConnected -= this.OnNetworkPeerConnected;
multiplayer.PeerDisconnected -= this.OnNetworkPeerDisconnected;
}
Or with an IDisposable
:
[OnEnterTree]
private IDisposable SetupMultiplayerEvents() {
var multiplayer = GetTree().GetMultiplayer();
multiplayer.PeerConnected += this.OnNetworkPeerConnected;
multiplayer.PeerDisconnected += this.OnNetworkPeerDisconnected;
return Disposable.Create(() => {
multiplayer.PeerConnected -= this.OnNetworkPeerConnected;
multiplayer.PeerDisconnected -= this.OnNetworkPeerDisconnected;
});
}
SceneUniqueName
Automatically resolve named nodes within children on enter tree, on fields or properties.
[SceneUniqueName("%MyLabel", required: false)]
private RichTextLabel? OptionalLabel { get; set; }
[SceneUniqueName("%MyLabel")]
private RichTextLabel Label { get; set; }
This resolves via GetNode
(or GetNodeOrNull
if not required) in a generated _EnterTree
, so you will get useful
godot errors if it is not found.
Technically you don't have to limit this to Unique Names (ie %...
), but that is what it was intended for and given it
is called in the EnterTree lifecycle you may need to be cautious.
AutoDispose
Wraps a method with an Update
version, and tracks cleanup via an IDisposable
.
[OnReady]
and [OnEnterTree]
above described some utility in returning
IDisposable
, however this allows disposable tracking for ad-hoc methods that will still get cleaned up.
For example
[AutoDispose]
private IDisposable Foo(Foo foo) {
this.foo = foo;
foo.Add(this);
return Disposable.create(() => {
foo.Remove(this);
this.foo = null;
});
}
// This will automatically create UpdateFoo and DisposeFoo methods.
public void UpdateFoo(Foo foo);
private void DisposeFoo();
Accessibility of the Update
method can be controlled with the accessibility
argument, eg
[AutoDispose(Accessibility = Accessibility.Private)]
.
DisposeFoo will be called on tree exit.
Other notes
Ordering of calls is by occurence within the source file, and in reverse on dispose (if applicable).
This means you likely want [SceneUniqueName]
fields and properties before any [OnEnterTree]
methods
that depend on them.
[SceneUniqueName("%MyNode")]
Node MyNodePopulatedFirst;
[OnEnterTree]
IDisposable FirstMethod() { /* ... */ }
[OnEnterTree]
IDisposable? SecondMethod() { /* ... */ }
[OnEnterTree]
IDisposable? ThirdMethod() { /* ... */ }
// Generated _EnterTree is conceptually:
public override void _EnterTree()
{
MyNodePopulatedFirst = GetNode("%MyNode");
_disposable_FirstMethod = FirstMethod();
SecondMethod();
_disposable_ThirdMethod = ThirdMethod();
}
// Generated _ExitTree is conceptually:
public override void _ExitTree()
{
_disposable_ThirdMethod?.Dispose();
_disposable_ThirdMethod = null;
_disposable_FirstMethod.Dispose();
_disposable_FirstMethod = null;
}
Using godothat
- Add the following property to your project's .csproj to disable Godot's standard ScriptMethods generator:
<GodotDisabledSourceGenerators>ScriptMethods</GodotDisabledSourceGenerators>
- Add the godohat nupkg dep to your project.
- Add annotations and enjoy
Acknowledgements
Godot Code / Inspiration
ScriptMethodsGenerator is a reimplementation godot's ScriptMethodsGenerator to produce similar output.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
0.4.7 | 112 | 6/26/2024 | |
0.4.6 | 103 | 6/26/2024 | |
0.4.5 | 226 | 12/16/2023 | |
0.4.4 | 150 | 12/16/2023 | |
0.4.3 | 177 | 12/2/2023 | |
0.4.2 | 294 | 10/28/2023 | |
0.4.1 | 254 | 8/6/2023 | |
0.4.0 | 216 | 8/5/2023 | |
0.3.2 | 151 | 7/30/2023 | |
0.3.1 | 176 | 7/16/2023 | |
0.1.21 | 185 | 7/9/2023 | |
0.1.10-dev-1-g5582169 | 266 | 7/9/2023 | |
0.1.9-dev-2-g2fa1d80 | 232 | 7/9/2023 | |
0.0.1-dev-2-gd85498b | 148 | 7/2/2023 | |
0.0.0-dev-3-g27db45c | 153 | 7/2/2023 |