Capturing Tile Positions
This guide explains how to capture tile coordinates such as Vector2i(5, 3) when an object is placed with Grid Building.
This is especially useful for:
- isometric pathfinding and movement
- occupancy maps
- save/load of grid-aligned gameplay state
- associating placed objects with one or more covered cells
Version note: This guide is validated for Grid Building 5.0.7.
Short Answer
The plugin gives you built-in ways to derive tile positions, but it does not automatically persist occupied tiles on the final placed object.
What exists already:
GBPositioning2DUtils.get_tile_from_global_position(...)converts a world position into aVector2itile.TileCheckRule.get_tile_positions()exposes indicator-covered tiles during rule evaluation.CollisionUtilities.get_rect_tile_positions(...)can derive a rectangular footprint in tile coordinates.BuildingState.pre_instance_addedlets you configure the placed instance before_ready()runs.
What you usually do in game code:
- capture the tile or tiles you need when placement commits
- store them on your own component, metadata, or save payload
- let your pathfinding or occupancy system read that data later
When To Capture Tile Positions
There are three common moments:
1. During preview or rule evaluation
Use this when you need tile coverage for validation visuals or per-tile rule logic.
Relevant API:
TileCheckRule.get_tile_positions()RuleCheckIndicator.get_tile_position(...)
2. Right before the final placed object is added
Use this when you want to stamp grid coordinates onto the committed instance.
Relevant API:
BuildingState.pre_instance_added
This is usually the best hook for pathfinding and occupancy data.
3. Later, from the placed object’s transform
Use this when you only need an anchor tile and can derive it from the final world transform.
Relevant API:
GBPositioning2DUtils.get_tile_from_global_position(...)
Single-Tile Anchor Pattern
If you only need one tile per placed object, store the anchor tile.
| |
This is the simplest pattern for:
- unit spawning
- interaction anchors
- isometric movement targets
- tile-based references for save/load
Notes for isometric games
This still works for isometric TileMapLayer setups because the helper uses:
map.to_local(global_position)map.local_to_map(...)
So it respects the map transform instead of assuming a square top-down grid.
Multi-Tile Footprint Pattern
If the placed object covers multiple cells, store an array of Vector2i values instead of a single anchor tile.
Option A: capture rule/indicator tiles
If your placement rules already generate the exact footprint you care about, use those tile positions during placement logic.
| |
This works well when your occupancy footprint matches the plugin’s indicator footprint.
Option B: derive from a rectangular footprint
If your object uses a rectangular footprint, use the collision utility helper.
| |
This is useful for:
- buildings with a known width and height
- pathfinding blockers
- rectangular occupancy maps
Option C: derive from your own collision or polygon data
If your footprint is not rectangular, store whatever tile set your own movement or occupancy system considers authoritative.
That may come from:
- collision shapes
- custom polygons
- tile rules
- a custom footprint resource
Best Hook: pre_instance_added
BuildingState.pre_instance_added is emitted after the scene is instantiated but before it is added to the tree.
That makes it the best place to stamp tile data onto the placed instance.
| |
You can replace metadata with a custom component or script property if you want stronger typing.
Recommended Occupancy Component Pattern
For pathfinding-heavy games, avoid recomputing occupied tiles everywhere. Store them once.
Example pattern:
| |
Then populate it when placement commits:
| |
For larger buildings, replace occupied_tiles with your full tile footprint array.
This gives your movement or pathfinding code a stable place to read from.
Save/Load Recommendation
The built-in PlaceableInstance.save() stores:
- instance name
- transform
- placeable reference
It does not automatically store tile positions.
If tile coordinates matter to your game state, merge them into your own save payload after calling the plugin save method.
| |
For multi-tile occupancy, save an array of serialized Vector2i values the same way.
Isometric Pathfinding Advice
For isometric games, the most practical setup is usually:
- Store one
anchor_tilefor interaction and identity. - Store
occupied_tilesfor blockers or large structures. - Feed
occupied_tilesinto your own pathfinding graph, AStarGrid2D layer, or occupancy map.
That keeps responsibilities clean:
- Grid Building handles placement.
- Your game handles movement and pathfinding.
Practical Recommendations
- Store tile positions on placement, not by repeatedly recomputing them everywhere.
- Use a single anchor tile for simple objects.
- Use an array of
Vector2itiles for blockers and multi-cell buildings. - Use
pre_instance_addedwhen you want the placed object configured before_ready(). - Add custom save data if tile occupancy matters after reload.