DeclarativeGodot 0.0.1
dotnet add package DeclarativeGodot --version 0.0.1
NuGet\Install-Package DeclarativeGodot -Version 0.0.1
<PackageReference Include="DeclarativeGodot" Version="0.0.1" />
<PackageVersion Include="DeclarativeGodot" Version="0.0.1" />
<PackageReference Include="DeclarativeGodot" />
paket add DeclarativeGodot --version 0.0.1
#r "nuget: DeclarativeGodot, 0.0.1"
#:package DeclarativeGodot@0.0.1
#addin nuget:?package=DeclarativeGodot&version=0.0.1
#tool nuget:?package=DeclarativeGodot&version=0.0.1
DeclarativeGodot
This is a highly experimental (and unfinished) package that adds Flutter-like Node tree building to Godot.
Usage
- Install the package via Nuget: https://www.nuget.org/packages/DeclarativeGodot.
- Create a new Godot project (or open an existing one).
- Set any scene as the main scene. The declarative tree will be created alongside its contents (a Godot limitation, since you can't run a game without a main scene).
- Add a new script with
DeclarativeGodot.DSceneTreeas the base class.- Add the
[GlobalClass]attribute. - Override the
_Initializemethod and callbase._Initialize(). - Override the
CreateRootmethod. Here you can start building yourWidgetDObjecttree.
- Add the
- In Godot settings, set this class as the current Main Loop (Project > Project Settings... > Application > Run > Main Loop Type).
- Profit! I guess?
Concept and implementation
When designing this package I attempted to recreate Flutter's declarative approach while keeping everything as simple as possible (you build your tree using nested Widgets (which are immutable) and when you want to make changes to it you call a function which rebuilds it).
The class hierarchy in DeclarativeGodot looks like this:
|- DSceneTree - a class which you need to extend for the whole thing to work; extends SceneTree
|- DObject - a base class for tree nodes, similar to Flutter's Widget
|- DWidget - a base class for creating your own nodes, similar to Flutter's StatelessWidget
|- DNodeBase - a base class for all the Godot Nodes (more on that later)
|- DNode
|- DNode2D
|- ...
|- DNode3D
|- ...
|- ...
The DObject class has the following public properties:
string? _Key- similar to Flutter'skeyproperty. Tells the declarative tree to reuse the underlyingNodeafter rebuilds.bool _RecreateNode- if set totrue, will force the tree to create a newNodeduring the rebuild. If this is set tofalse(thebooldefault) and the_Keytonull(also the default), the tree will attempt to reuse a randomNodeof a corresponding type.
Unlike Flutter, Godot allows each Node to have any number of child nodes. That's why the base DObject class has a IEnumerable<DObject> GetChildren() method. DNodeBase implements this via a Children property that you can assign. DWidget implements it via the Build method.
Now the fun part - the declarative Godot Node counterparts. For this, source generation was used. The source generator would go over every type in the GodotSharp package which extends Node, and generate a D* wrapper (DNode, DLabel, DSprite2D etc). These wrappers expose all the same properties that the underlying node has. When the tree is (re)built, those properties will then be (re)assigned to the underlying node (which could be a newly created one or a reused one). All these properties are nullable. If a property is null (meaning it wasn't explicitly set), it will not be assigned, keeping the default/reused value of the Node.
Additionally, for all the events (Signals) of the underlying Node, a property is created (with an On* prefix) and automatically subscribed/unsubscribed.
To rebuild the tree call Rebuild from a DObject subclass or directly on your DSceneTree subclass.
Code example
MyCustomTree.cs:
using DeclarativeGodot;
using Godot;
[GlobalClass]
public partial class MyCustomTree : DSceneTree {
public override void _Initialize() =>
base._Initialize();
public override DObject CreateRoot() =>
new MyCustomWidget();
}
MyCustomWidget.cs:
using DeclarativeGodot;
using Godot;
public class MyCustomWidget : DWidget {
protected override DObject Build() =>
new DControl() {
OnReady = () => GD.Print("The widget is ready!"),
Children = [
new DVBoxContainer() {
Children = [
new DLabel() {
Text = "Hello world!",
},
new DButton() {
Text = "Exit",
OnPressed = () => (Engine.GetMainLoop() as SceneTree)?.Quit()
}
]
}
]
};
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- GodotSharp (>= 4.3.0)
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.0.1 | 283 | 3/16/2025 |