Integrate Godot
Build and run
Use Godot 4.7.2 .NET and the repository SDK. Run from the repository root:
dotnet build Samples/GodotIntegration/GodotIntegration.csproj -c Debug
godot --headless --editor --path Samples/GodotIntegration --import
godot --path Samples/GodotIntegration res://GettingStarted/QuickStart.tscnThe window displays a resource title selected by key. Open the dialog and choose an action. The status shows Confirmed or Cancelled.
Compose services and views
Provide exported resources and views in the scene root. Register the resource component, native mount root, and typed routes in the composition root. After resource initialization, the RegisterGodotScene<QuickStartPage, int> callback looks up its resource and configures the detached page.
// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using System.Threading;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.Architectures;
using GFramework.Core.Architectures;
using GFramework.Game.Abstractions.Runtime;
using GFramework.Game.Abstractions.UI;
using GFramework.Game.Runtime;
using GFramework.Game.UI;
using GFramework.Godot.UI;
using Microsoft.Extensions.DependencyInjection;
namespace GodotIntegration.GettingStarted;
/// <summary>Composes ordinary services, explicitly ordered startup and the borrowed native host.</summary>
/// <remarks>All container access stays here. Native attachment follows resource validation; detachment follows UI stop.</remarks>
/// <param name="root">The borrowed scene and resource owner.</param>
/// <param name="dispatcher">The owning Godot thread.</param>
internal sealed class QuickStartArchitecture(QuickStartRoot root, IGameDispatcher dispatcher) : Architecture, IAsyncArchitectureHost
{
/// <summary>Describes dependencies and startup participants before any service is activated.</summary>
protected override void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(root);
services.AddSingleton(dispatcher);
services.AddComponent<QuickStartResources>(order: -100);
services.AddSingleton(provider => CreateRoutes(provider.GetRequiredService<QuickStartResources>()));
services.AddModule(new GameRuntimeModule(dispatcher, runtime =>
{
runtime.AddSingleton<IUiService>(provider => new UiService(provider.GetRequiredService<UiRegistry>(),
root.UiRoot, dispatcher));
}));
}
private UiRegistry CreateRoutes(QuickStartResources resources)
{
var routes = new UiRegistry();
routes.RegisterGodotScene<QuickStartPage, int>(QuickStartRoot.PageRoute, root.PageScene, (page, _, _) =>
page.Configure(resources.Get("welcome"), root.RequestConfirmation));
routes.RegisterGodotDialog(QuickStartRoot.ConfirmRoute, (question, context) =>
{
var node = root.ConfirmScene.Instantiate();
try
{
var confirm = (QuickStartConfirm)node;
confirm.Configure(question, context, root.RunResponse);
return confirm;
}
catch
{
node.Free();
throw;
}
});
return routes;
}
/// <inheritdoc />
ValueTask IAsyncArchitectureHost.StartHostAsync(CancellationToken cancellationToken) =>
new(dispatcher.InvokeAsync(() => root.AttachUiAsync(Services.GetRequiredService<IUiService>()), cancellationToken));
/// <inheritdoc />
protected override ValueTask OnStoppingAsync() => new(dispatcher.InvokeAsync(root.StopActionsAsync));
/// <inheritdoc />
protected override ValueTask OnStoppedAsync() => new(dispatcher.InvokeAsync(root.DetachUiAsync));
}Return a result and close
Register the dialog factory with RegisterGodotDialog and pass actions and context to the view. Confirm completes the bool result and Close dismisses the current dialog. The host stops the UI before it quits the scene tree.
// Copyright (c) 2025-2026 GeWuYou
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Threading.Tasks;
using GFramework.Game.Abstractions.UI;
using Godot;
namespace GodotIntegration.GettingStarted;
/// <summary>Completes its typed result through a host-observed response that can run while the opener waits.</summary>
public partial class QuickStartConfirm : CenterContainer
{
private string _question = string.Empty;
private UiDialogContext<bool> _context = null!;
private Action<Func<Task>> _run = null!;
/// <summary>Receives the dialog's own completion context before entering the tree.</summary>
/// <param name="question">Opening argument.</param>
/// <param name="context">The owning UI instance.</param>
/// <param name="run">Observed response admission.</param>
public void Configure(string question, UiDialogContext<bool> context, Action<Func<Task>> run)
{
_question = question;
_context = context;
_run = run;
}
/// <inheritdoc />
public override void _Ready()
{
GetNode<Label>("Panel/Content/Question").Text = _question;
GetNode<Button>("Panel/Content/Accept").Pressed += () => _run(() => _context.CompleteAsync(result: true));
GetNode<Button>("Panel/Content/Cancel").Pressed += () => _run(() => _context.CloseAsync());
}
}Smoke test
godot --headless --path Samples/GodotIntegration res://GettingStarted/QuickStart.tscn -- --quickstart-smokeThe smoke test covers page binding, cleanup after scene registration failures, confirmation, dismissal, and shutdown. Next, run the full game.