GodotExt 0.1.0
See the version list below for details.
dotnet add package GodotExt --version 0.1.0
NuGet\Install-Package GodotExt -Version 0.1.0
<PackageReference Include="GodotExt" Version="0.1.0" />
paket add GodotExt --version 0.1.0
#r "nuget: GodotExt, 0.1.0"
// Install GodotExt as a Cake Addin #addin nuget:?package=GodotExt&version=0.1.0 // Install GodotExt as a Cake Tool #tool nuget:?package=GodotExt&version=0.1.0
GodotExt - Extension methods for the Godot C# API
This library contains a set of useful extension methods that augment Godot's C# API to make it easier to use and more type-safe. The extension methods are defined for a handful of types.
Installation
GodotExt is published on NuGet. To add it use this command line command (or the NuGet facilities of your IDE):
dotnet add package GodotExt --version 0.1.0
What's inside
Finding nodes
You can use the AtPath
and WithName
extension methods as a type-safe and verified replacement for Godot's GetNode
and FindNode
methods. Both replacements will verify that the node exists and throw an exception if it doesn't.
var button = GetNode<Button>("Some/Path/To/Button");
// becomes
var button = this.AtPath<Button>("Some/Path/to/Button");
var button = FindNode<Button>("Button", true, false);
// becomes
var button = this.WithName<Button>("Button");
Parent-child relationships
GetChildNodes<T>
is a type-safe replacement for GetChildren
. It will only return nodes of the given type back:
foreach(var child in this.GetChildren()) {
if (child is Button button) {
button.Disabled = true;
}
}
// becomes
foreach(var button in this.GetChildNodes<Button>()) {
button.Disabled = true;
}
MoveToNewParent
moves a node to a new parent:
if (player.GetParent() != null) {
player.GetParent().RemoveChild(player);
}
airship.AddChild(player);
// becomes
player.MoveToNewParent(airship);
RemoveFromParent
removes a node from its parent:
if (node.GetParent() != null) {
node.GetParent().RemoveChild(node);
}
// becomes
node.RemoveFromParent();
RemoveAndFree
is a safer alternative to QueueFree
. It ensures the node is actually removed from the tree before QueueFree
is called, which helps avoiding strange behaviour when the node is still in the tree for the remainder of the current frame:
if (node.GetParent() != null) {
node.GetParent().RemoveChild(node);
}
node.QueueFree();
// becomes
node.RemoveAndFree();
Nodes in groups
FindBelowInGroup
finds all nodes below a given node that are in a given group:
var myNode = ...; // this is the node below which we want to find all nodes that are in a certain group
var nodesInGroup = GetTree().GetNodesInGroup("SomeGroup");
var result = new List<Node>();
foreach (var node in nodesInGroup) {
if (myNode.IsAParentOf(node)) {
result.Add(node);
}
}
// becomes
var nodesInGroup = myNode.FindBelowInGroup("SomeGroup");
GetNodesInGroup
gets an override that will filter the group nodes to only return nodes of the given type:
var enemies = GetTree().GetNodesInGroup("SomeGroup")
.OfType<Enemy>();
// becomes
var enemies = GetTree().GetNodesInGroup<Enemy>("SomeGroup");
Signals
FiresSignal
provides a better-readable alternative to the built-in ToSignal
when you want to wait for a certain signal with await
.
await ToSignal(timer,"timeout");
// becomes
await timer.FiresSignal("timeout");
Timers and Tweens
For timers and tweens there are additional methods that make using them easier:
// make a one-off 500ms timer and wait on it
await ToSignal(GetTree().CreateTimer(0.5), "timeout");
// becomes
await this.Sleep(0.5);
await ToSignal(timer, "timeout");
// becomes
await timer.Timeout();
await ToSignal(tween, "tween_completed");
// becomes
await tween.IsCompleted();
Fluent API for signal connections
Connect
gets an override which allows you to fluently create signal connections:
button
.Connect(
"pressed",
this, nameof(OnButtonPressed)
);
// becomes
button
.Connect("pressed")
.To(this, nameof(OnButtonPressed));
In addition to the vanilla API it also directly checks for errors during binding and throws an exception if the binding fails instead of silently failing. Also, this API allows for easier additional binds and binding flags, which can use the actual enum's instead of uint
s and don't require you to manually create Godot's Array
instances:
button.Connect(
"pressed",
this, nameof(OnButtonPressed),
new Array(button),
((uint) ConnectFlags.Deferred) | ((uint) ConnectFlags.OneShot)));
// becomes
button
.Connect("pressed")
.WithBinds(button)
.WithFlags(ConnectFlags.Deferred, ConnectFlags.OneShot)
.To(this, nameof(OnButtonPressed))
The fluent API also returns a binding object which allows you to easily disconnect the binding again without having to write all the parameters again:
button.Connect("pressed", this, nameof(OnButtonPressed));
// later...
button.Disconnect("pressed", this, nameof(OnButtonPressed));
// becomes
var connection = button
.Connect("pressed")
.To(this, nameof(OnButtonPressed));
// later...
connection.Disconnect();
Miscellaneous
Type-safe cloning of nodes
Clone
is a type-safe variant of Duplicate
:
var monsterClone = monster.Duplicate() as Monster;
// becomes
var monsterClone = monster.Clone(); // no casting necessary
Snake-case conversion for CallDeferred
When calling Godot-API through CallDeferred
you need to translate the method name into snake-case. This can easily introduce typos. Therefore string
gets an extension method that converts the string to snake-case:
someNode.CallDeferred("someMethod", new Array(1, 2, 3));
// becomes
someNode.CallDeferred(nameof(SomeNode.SomeMethod).ToSnakeCase(), new Array(1, 2, 3));
As an additional bonus the deferred call will now also appear in the usage search of your IDE.
2D World-Screen transformations on ViewPort
Viewport
has some extensions that allow you to quickly transform screen coordinates to a world coordinates:
var worldCoordinates = GetGlobalMousePosition() * viewport.CanvasTransform;
// becomes
var worldCoordinates = viewport.ScreenToWorld(GetGlobalMousePosition());
and the reverse is also possible:
var playerPositionOnScreen = viewport.CanvasTransform * player.position;
// becomes
var playerPositionOnScreen = viewport.WorldToScreen(player.position);
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETFramework 4.7.2
- JetBrains.Annotations (>= 2022.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on GodotExt:
Repository | Stars |
---|---|
derkork/openscad-graph-editor
OpenSCAD Graph Editor
|
This is an early version. While it is working API may change.