# Build a Spatial Grid Inventory

> For the complete documentation index, see [llms.txt](llms.txt).

Build a three-column, two-row backpack. A small supply occupies one cell. A tool occupies two neighboring cells and can turn to fit vertically. Start with a working player Inventory Manager from [First Steps](inv-first-steps.html). You can use this layout as the player's first bag.

Procedure: Define the backpack and its contents

1. Create a Container Definition called Container_FieldPack. Add `Inventory.Container.Trait.Default` and choose Spatial Layout. Use Property for the slot count, set Grid Width to three and Grid Height to two, and use Per Cell placement. Set Swap Policy to Blocked for this test. Leave its item compatibility query empty.

2. Assign the field pack as the only Initial Containers entry on the player's Inventory Manager. Clear the starting items for this test. Use a fresh inventory so the old bag does not receive pickups.

3. Create two Item Definitions, Item_Supply and Item_Tool. Give both a Container Placement Fragment, a User Interface Fragment with distinct icons, and a Dimensions Fragment. Set the supply to one cell wide by one high and the tool to two wide by one high. Enable Can Rotate on the tool. Leave stacking off or set the stack limit to one.

4. Set up individual [pickups](inv-guide-inventory-items.html) for four supplies and one tool. For an irregular item, enable the Dimensions Fragment's Occupancy Mask and mark the cells the item actually covers. Leave the mask off for the rectangular tool.

Procedure: Draw cells and items in the same space

1. Create a Widget Blueprint based on `NinjaInventorySpatialContainerWidget`. Assign Container_FieldPack as its Container Data Asset, enable Fill Empty Entries, and add an Inventory Container ViewModel using Inventory Resolver.

2. In the Designer, add an Overlay named `GridOverlay`. Inside it, place a Uniform Grid Panel named `ReferenceGrid` first and a Grid Panel named `ItemGrid` above it. Give both panels the same bounds and let pointer hits pass through empty space in the top panel to its underlying cells (set the top panel to Not Hit-Testable (Self Only)). The bottom panel supplies one target per cell. The top panel draws each item once at its starting cell and spans its width and height.

3. For Reference Widget Class, use a slot widget with a visible cell border and a consistent cell size. For Item Widget Class, use an item widget with the item's icon. Add a manual Inventory Item ViewModel to both. Bind the container and position on the cell widget and the item view on the icon widget as shown in [Bindings with the Item Widget](inv-ui-mvvm.html#bindings-with-the-item-widget). In the icon widget, use On Item View Set to get the source item and read Get Dimensions from its Dimensions Fragment. Multiply width and height by the cell size and apply them to the icon's Size Box. With 48-unit square cells, the tool should measure 96 by 48 or 48 by 96.

4. Bind the container ViewModel's Get Items to the spatial widget's Set Grid Entries. Add the widget to the player's menu. Enable [drag-and-drop](inv-ui-drag-and-drop.html) on the cells and items so you can rearrange the contents.

For a Rotate action on a stored item, get its Dimensions Fragment and call `CanRotateItemInPlace`. If it fits, call `SetRotated` with the opposite of `IsRotated`. For a request through another authorized inventory, use `SetRotatedFromAuthorizedInventory` with the requesting manager.

To redraw immediately after a turn, bind the bag screen to the manager's `OnItemEvent`. When the tag is `Inventory.Event.Item.RotationChanged` and the item belongs to this bag, refresh its entries on the next tick. In Blueprint, keep the container reference, call Set Container on the container ViewModel with an empty reference, then set it back to the saved container. The Get Items binding rebuilds both layers so the old cells become available as drop targets. Unbind the screen when it closes.

A C++ layout subclass can override `TestWithCustomAlgorithm` and select the layout's Custom placement algorithm if your game uses different fit rules.

For an example with gaps, this L-shaped item can turn once the smaller item moves out of its way.

![A small item blocks an L-shaped item from turning](Documentation/inv_layout_spatial.png)
![The L-shaped item rotated after the blocking cell was cleared](Documentation/inv_layout_spatial_rotated.png)

## Try It in Play

1. Collect the four supplies. Arrange three across the bottom row and one in the top middle cell. Pick up the horizontal tool. Both top corners are free, but the tool needs two adjacent cells. It should stay in the world.

2. Move the top supply to the top-left corner. Pick up the tool again. It should occupy the top-middle and top-right cells, with one icon across both.

3. Try rotating it. The supply directly below its starting cell blocks the turn. Free that bottom-middle cell and rotate again. The tool should now cover the middle column and leave the top-right cell free.

