Development ⚠️ GridPlacement 6.0 documentation is in active development. APIs and content may change, and the site may be temporarily unstable.

Placeable Selection UI (C# 6.0)

Overview

This guide shows how to use the Placeable Selection UI in the GridPlacement 6.0 C# plugin.

The goal is to:

  • Display a list/grid of available placeables (single items and upgrade sequences).
  • Let the player pick a placeable.
  • Enter build mode for the selected placeable using the 6.0 services.

The implementation uses:

  • Core services:
    • GridBuilding.Core.Catalog.IPlaceableCatalog
    • GridBuilding.Core.Catalog.SelectionAdapter
    • GridBuilding.Core.Services.Placement.IPlacementService (backend placement)
    • GridBuilding.Core.Services.PlacementModeService (“which thing am I trying to place?”)
  • Godot adapters:
    • GridBuilding.Godot.Bootstrap.ServiceCompositionRoot
    • GridBuilding.Godot.UI.PlaceableSelectionPanel

1. Ensure ServiceCompositionRoot is in your scene

Add ServiceCompositionRoot as an autoload or as a root node in your main scene:

  1. In Godot, add the C# script GridBuilding.Godot.Bootstrap.ServiceCompositionRoot to a Node.
  2. Mark it as an autoload (recommended) or place it at the top of your game scene.

At runtime this will:

  • Create a ServiceRegistry.
  • Register core services:
    • GridBuilding.Core.Services.Placement.PlacementService
    • GridBuilding.Core.Catalog.PlaceableCatalog (seeded via PlaceableCatalogBootstrap)
    • GridBuilding.Core.Services.PlacementModeService
  • Register Godot services:
    • GridBuilding.Godot.Services.Placement.SceneService

2. Add a PlaceableSelectionPanel to your UI

In your UI scene (e.g. HUD or build menu):

  1. Create a Control node.
  2. Attach the C# script GridBuilding.Godot.UI.PlaceableSelectionPanel.
  3. (Optional) Set InitialCategoryFilter in the inspector, e.g. buildings.

On _Ready, the panel will:

  • Resolve ServiceRegistry from ServiceCompositionRoot.
  • Resolve IPlaceableCatalog and PlacementModeService from the registry.
  • Create a SelectionAdapter and call Initialize().
  • Apply the optional InitialCategoryFilter.
  • Connect the adapter’s selection to the Godot signal PlaceableSelected.

Note: The panel does not create buttons automatically. It exposes the catalog data and selection signal so you can build your own UI layout.

3. Building your actual UI grid

From GDScript or C# (Godot side), you can:

  • Read the available items: panel.Adapter.AvailableItems.
  • Read filtered items: panel.Adapter.FilteredItems.
  • Connect to PlaceableSelected to react when a player picks something.

Example (C# script on a parent UI node):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using Godot;
using GridBuilding.Godot.UI;
using GridBuilding.Core.Catalog;

public partial class BuildMenu : Control
{
    private PlaceableSelectionPanel? _panel;

    public override void _Ready()
    {
        _panel = GetNode<PlaceableSelectionPanel>("PlaceableSelectionPanel");
        if (_panel?.Adapter == null)
        {
            GD.PushError("BuildMenu: PlaceableSelectionPanel not initialized.");
            return;
        }

        _panel.PlaceableSelected += OnPlaceableSelected;

        foreach (var entry in _panel.Adapter.FilteredItems)
        {
            // TODO: Instantiate buttons or grid cells and bind to entry.Id / entry.DisplayName.
        }
    }

    private void OnPlaceableSelected(string placeableId)
    {
        GD.Print($"BuildMenu: Player selected {placeableId}, entering build mode.");
        // PlacementModeService is already updated by SelectionAdapter.
        // Your targeting/manipulation code should read from PlacementModeService
        // when deciding what to preview/place.
    }
}

4. How this interacts with placement and targeting

  • SelectionAdapter calls PlacementModeService.BeginPlacement(placeableId).
  • PlacementModeService stores CurrentPlaceableId and flips IsInPlacementMode = true.
  • Your targeting / manipulation systems should query PlacementModeService to know:
    • Whether the player is in build mode.
    • Which placeable ID to resolve to a Placeable and preview.
  • When the player cancels build mode, call PlacementModeService.CancelPlacement() (e.g. via an input handler or UI cancel button).

The actual backend placement is still performed by GridBuilding.Core.Services.Placement.IPlacementService and related command services. The selection UI and PlacementModeService are responsible only for which thing the player wants to place next.

5. Overriding the default catalog

The default catalog is populated by PlaceableCatalogBootstrap.SeedDefaults. For a real game you will usually:

  • Define your own data source (JSON, scriptable configs, DB, etc.).
  • Replace the catalog registration in your own composition root, or
  • Extend ServiceCompositionRoot.RegisterCoreServices in a fork.

Example override (pseudocode):

1
2
3
var catalog = new PlaceableCatalog();
MyGameCatalogLoader.Populate(catalog);
_serviceRegistry.RegisterSingleton<IPlaceableCatalog, PlaceableCatalog>(catalog);

As long as you implement IPlaceableCatalog and keep using SelectionAdapter, the Placeable Selection UI will keep working.