Placement Rules
This guide covers runtime placement validation. For configuration and runtime-readiness checks, see Validation: Configuration. For writing your own rules, see Custom Placement Rules.
Version note: This guide is validated for Grid Building 5.0.8.
TileCheckRule API Contract (5.0.8)
⚠️ COMMON BUG ⚠️
get_runtime_issues()is NOT called automatically byget_failing_indicators(). If your rule returns failures fromget_runtime_issues()but indicators show green, you forgot to overrideget_failing_indicators().See
test/demos/building/my_grid_bounds_rule_bug_test.gdfor a full reproduction andcustom-placement-rules.mdfor the fix pattern.
TileCheckRule has three methods that control indicator state:
| Method | Called by system? | Controls indicator? | Use for |
|---|---|---|---|
validate_placement() | ✅ via get_failing_indicators() | ✅ yes | Pass/fail validation |
get_runtime_issues() | ❌ not automatic | ❌ no (unless you override get_failing_indicators()) | Diagnostics |
get_failing_indicators() | ✅ yes | ✅ yes | Override to call get_runtime_issues() |
The critical fix in 5.0.8: get_runtime_issues() is NOT called automatically by get_failing_indicators(). If your custom rule overrides get_runtime_issues() and expects it to control indicator display, you MUST also override get_failing_indicators() to call it.
| |
Also fix for 5.0.8: When overriding get_runtime_issues(), call super.get_runtime_issues() BEFORE appending custom issues. This prevents your issues from being lost when the base class returns early.
| |
What Placement Rules Are
Placement rules are resources that validate whether an object can be placed at the current target. Each rule:
- Receives the active targeting state via
setup(p_gts: GridTargetingState). - Returns a
RuleResultfromvalidate_placement(). - May run post-success side effects in
apply().
Rule Sources
There are two rule layers in the 5.0.8 runtime, combined during preview creation:
Base Rules (GBSettings)
- Configured on
GBSettings.placement_rules. - Apply broadly to all placement workflows.
- Set once on the composition container.
Placeable-Specific Rules (Placeable resource)
- Configured on each
Placeableviaplaceable.placement_rules. - Apply only to that placeable’s preview/placement flow.
- Can include
placeable.ignore_base_rules = trueto skip base rules.
Rule Combination Flow
When enter_build_mode() is called, rules are combined via PlacementValidator.get_combined_rules():
| |
The combination logic (PlacementRuleValidationLogic.combine_rules()):
- If
ignore_base_rules = false: append base rules first - Always append placeable rules
- Deduplicate while preserving order
Ignore Base Rules
If Placeable.ignore_base_rules = true, base rules are skipped — only placeable-specific rules apply.
Indicator Creation During Preview
When a preview is created (either for placement or manipulation), indicators are spawned and wired to rules:
Placement Flow
| |
Manipulation Flow
| |
Both flows use the same indicator creation path. The only difference is:
- Placement: rules come from
placeable.placement_rules - Manipulation: rules come from
source.get_move_rules() - Manipulation additionally sets
collision_exclusions = [source.root]to exclude the original object
Rule → Indicator Wiring
Each RuleCheckIndicator holds references to its rules via add_rule(). When update_validity_state() runs:
| |
How Indicators Are Generated From Collision Shapes
Indicators are not magical — they are created at positions derived from collision shapes on your placeable’s packed_scene. Understanding this pipeline is essential for diagnosing silent failures.
The Indicator Generation Pipeline
| |
Layer Configuration: Two Separate Settings
1. collision_layer on your placeable’s nodes — “What layer this object IS”
| |
2. collision_mask on TileCheckRules (like CollisionsCheckRule) — “What layers this rule CHECKS”
| |
Why Misassigned Layers Cause Silent Failures
| Scenario | collision_layer | rule collision_mask | Result |
|---|---|---|---|
| Correct | 1, 10 | 10 | ✅ Rule detects object |
| Wrong - object not on target layer | 1 only | 10 | ❌ Rule finds nothing — placement passes vacuously |
| Wrong - rule doesn’t look for layer | 1, 10 | 1 | ❌ Rule looks for wrong layer — finds nothing |
Example of silent failure:
| |
Special Case: Packed Scenes With No Collision Shapes
If your placeable’s packed_scene has no CollisionObject2D or CollisionShape2D nodes:
- No indicators are generated — the system has no positions to create indicators at
- No
TileCheckRulerules run — rules likeWithinTileMapBoundsRule,CollisionsCheckRule, andValidPlacementTileRulehave nothing to validate - All tile-based validation is bypassed — this is logically correct but can be surprising
This can be intentional for objects that:
- Are purely visual (no gameplay collision)
- Use a custom validation mechanism (e.g., raycasts from a different system)
- Should always pass tile-based checks regardless of position
If you want tile-based validation but have no collision shapes:
- Add collision shapes to your placeable’s packed scene (recommended)
- Use
VisualBoundsFallbackforWithinTileMapBoundsRuleonly — other rules still won’t work - Create a custom
TileCheckRulethat uses a different position source (advanced)
Manipulation vs Placement
| Aspect | Placement | Manipulation |
|---|---|---|
| Rules source | placeable.placement_rules | source.get_move_rules() |
| ignore_base from | placeable.ignore_base_rules | source.settings.ignore_base_rules |
| Collision exclusions | Default (none) | [source.root] — excludes original object |
| Preview instance | New instance created | Copy of source normalized to identity |
Both use IndicatorManager.try_setup() with the same rule combination logic.
\n5.0.4 update: CollisionCheckRule now includes a manual post-cast exclusion filter to suppress false-positive collisions during manipulation moves. Godot silently ignores ShapeCast2D.add_exception() when the cast origin is outside the excluded body’s bounds.
Core Rule Classes
| Class | Purpose |
|---|---|
PlacementRule | Base class for all placement rules |
TileCheckRule | Base class for rules that evaluate tile/indicator state |
RuleResult | Contains validation outcome and issues |
Built-in Rules
Grid Building 5.0.8 includes these built-in placement rules:
| Rule Class | Base Class | Purpose |
|---|---|---|
WithinTileMapBoundsRule | TileCheckRule | Restricts placement to valid tilemap cells |
CollisionsCheckRule | TileCheckRule | Checks for overlapping physics |
ValidPlacementTileRule | TileCheckRule | Basic validity check |
SpendMaterialsRuleGeneric | PlacementRule | Consumes inventory/materials after successful placement; reusable generic inventory-spend rule |
Rule Lifecycle
1. setup(p_gts: GridTargetingState) -> Array[String]
Called before the rule is used. PlacementRule.setup(...) stores the targeting state and marks the rule ready.
2. validate_placement() -> RuleResult
Called during validation. This is where the rule decides pass/fail.
⚠️ CRITICAL:
validate_placement()MUST be overridden in your custom rule. The base class implementation returns a failure by default with the message “This is a virtual condition function…”. If you don’t override it, your rule will always fail validation.
For the full custom-rule contract, including get_setup_issues(), get_runtime_issues(), and TileCheckRule indicator behavior, see Custom Placement Rules.
\n5.0.4 update: CollisionCheckRule now includes a manual post-cast exclusion filter to suppress false-positive collisions during manipulation moves. Godot silently ignores ShapeCast2D.add_exception() when the cast origin is outside the excluded body’s bounds.
3. apply() -> Array[String]
Called after successful placement if the workflow uses the apply phase for side effects.
4. tear_down() -> void
Called when the preview/rule evaluation cycle is reset or completed.
Writing Custom Rules
If you are defining your own rules, use the dedicated guide:
That guide covers:
- Godot 4.4-compatible custom rule patterns
- When to extend
PlacementRulevsTileCheckRule get_setup_issues()vsget_runtime_issues()TileCheckRuleindicator fallback behavior- Common authoring mistakes and tested expectations
Example: Generic Inventory Spend Rule
SpendMaterialsRuleGeneric is the reusable rule to use when placement should deduct materials from the player’s inventory or resource stack after a successful placement.
Use it when:
- The cost belongs to the placement flow, not the UI.
- The spend should happen only after validation passes.
- Your inventory lives under the owner root or a node the rule can locate through its configuration.
- You want one shared cost rule instead of custom spend logic on each placeable.
Good fit examples:
- A house costs wood and stone.
- A trap costs gold after a valid placement.
- A build action spends a generic resource stack from the owning player.
Keep the rule generic and let placeables or config supply the actual cost data. That keeps cost handling consistent across guides, demo scenes, and future placeables.
Built-in Rule Behavior Plugin Users Should Know
WithinTileMapBoundsRule
- Extends
TileCheckRule. - Checks each indicator against the active
TileMapLayer. - Fails when indicator cells resolve to no
TileData.
Visual Bounds Fallback
When no collision shapes are detected on a placeable object, WithinTileMapBoundsRule automatically falls back to using visual component bounds for validation. This fallback:
- Detects visual components: Searches for
Sprite2DandPolygon2Dnodes in the object hierarchy. - Calculates bounding box: Uses
VisualBoundsHelper.get_visual_bounding_box()to compute the union of all visual component rectangles. - Checks corner points: Validates that all four corners of the bounding box are over valid tiles.
When the fallback triggers:
- The rule has no indicators (collision shapes not detected).
- The object contains visual components (
Sprite2Dwith texture orPolygon2Dwith polygon data). - No collision region is defined for the object.
Why collision regions are recommended: While the visual bounds fallback provides basic validation, implementing explicit collision regions is strongly recommended for the following reasons:
- Precise control: Collision shapes allow you to define the exact footprint of your object, independent of visual representation.
- Accurate validation: Visual bounds may include transparent areas or visual effects that don’t represent the actual placement footprint.
- Performance: Collision-based detection is more efficient than traversing node hierarchies for visual components.
- Consistency: Using collision shapes ensures consistent behavior across all placement rules (
WithinTileMapBoundsRule,CollisionCheckRule,ValidPlacementTileRule). - Flexibility: You can create complex collision shapes (concave polygons, multiple shapes) that accurately represent your object’s placement requirements.
Visual bounds fallback limitations:
- May include non-solid visual areas in validation.
- Cannot represent complex footprints (L-shapes, concave areas).
- Less performant for objects with many visual components.
- Visual effects (particles, animations) may cause unexpected validation behavior.
ValidPlacementTileRule
- Extends
TileCheckRule. - Validates that tiles have required custom data fields and matching values.
- Does NOT have a visual bounds fallback — requires collision shapes for indicator generation.
Important: No Visual Bounds Fallback
Unlike WithinTileMapBoundsRule, ValidPlacementTileRule does not fall back to visual bounds when collision shapes are missing. This rule requires collision-based indicators to function properly.
Why collision regions are critical for ValidPlacementTileRule:
- No fallback mechanism: Without collision shapes, the rule cannot generate indicators and will fail to validate.
- Tile data validation: The rule checks specific tile custom data fields (e.g., “buildable”, “walkable”) which require precise tile position detection.
- Multiple tile coverage: Collision shapes define exactly which tiles need to have matching custom data.
- Complex footprint support: Buildings often span multiple tiles, and collision shapes accurately represent which tiles must be validated.
If you don’t implement collision regions for ValidPlacementTileRule:
- The rule will have no indicators to check.
- Validation will pass vacuously (no indicators = no violations) — this is logically correct but provides no validation.
- You lose the ability to validate that all covered tiles have the required custom data.
- Placement may succeed on tiles that shouldn’t be valid for building.
Why vacuous truth is the correct behavior:
- The rule checks tile custom data on a per-tile basis.
- If there are no collision shapes, there are no tiles to check.
- No tiles to check means no violations can exist.
- This is semantically correct: “all covered tiles have valid data” is true when there are no covered tiles.
- A visual bounds fallback would be inappropriate because visual bounds don’t tell you which tiles to check for custom data.
CollisionsCheckRule
- Extends
TileCheckRule. - Checks each indicator with a shapecast collision mask.
- Uses
collision_exclusionsfromGridTargetingState(not on the rule itself) to exclude preview bodies and other nodes. - Supports both clear-space and required-overlap flows through
pass_on_collision.
\n**Note: collision_exclusions are configured on GridTargetingState.collision_exclusions, not on the rule. See Targeting Flow for details.
5.0.4 update:
- Added
_ensure_messages()lazy-loading safeguard to prevent null derefs when nested resources fail to deserialize in exported builds. - Fixed
is_instance_validcheck missing — prevents accessing freed indicator objects. - Added manual post-cast exclusion filter to work around Godot
ShapeCast2D.add_exception()outside-bounds bug. - Templates now use
collision_mask = 1andapply_to_objects_mask = 1for project-agnostic defaults.
Practical Guidance
- Always call
super.setup(...)if you override setup. - Use
TileCheckRulewhen your rule depends on indicator positions or tilemap cells. - Use
PlacementRuledirectly when the rule depends on owner/inventory/game-state logic. - Use
SpendMaterialsRuleGenericwhen the rule needs to deduct resources after successful placement. - Keep side effects in
apply()if they should only happen after successful placement. - Put rules that should affect every placement into
GBSettings.placement_rules.
Common Mistakes
- Not overriding
validate_placement()— The base class returns a failure by default. Custom rules MUST override this method to returnRuleResult.build(self, [])for success. - Forgetting to call
setup(...)before validation. - Treating rules as editor-only resources instead of runtime logic.
- Duplicating grid-position logic in UI instead of reading targeting state.
- Putting placeable-specific rules into global settings when they should live on the
Placeable.