This guide covers runtime placement validation — rules that run when players attempt to place objects. For configuration validation at startup, see Validation Tools.
What Are Placement Rules?
Placement rules are components that validate whether an object can be placed at a specific location. Each rule:
- Receives the current targeting state via
setup(p_gts: GridTargetingState) - Returns a
RuleResultwith success/failure messages - Optionally performs actions in
apply()after successful placement
Core Rule Classes
| Class | Purpose |
|---|---|
PlacementRule | Base class for all placement rules (v5.0) |
TileCheckRule | Base class for rules that evaluate against TileMapGrid |
RuleResult | Contains validation outcome and messages |
Built-in Rules (v5.0)
GridBuilding 5.0.2 includes these built-in placement rules:
| Rule Class | Base Class | Purpose | Key Properties |
|---|---|---|---|
WithinTilemapBoundsRule | TileCheckRule | Restricts placement to tilemap bounds | Uses tilemap’s used_rect |
CollisionsCheckRule | TileCheckRule | Checks for overlapping physics | apply_to_objects_mask, collision_mask |
ValidPlacementTileRule | TileCheckRule | Basic validity check | Uses ValidPlacementTileRuleSettings |
SpendMaterialsRuleGeneric | PlacementRule | Consumes inventory items | cost_map, inventory_component_path |
Example: Custom Grid Bounds Rule (TileCheckRule)
For rules that evaluate against the TileMap, extend TileCheckRule. In 5.0.2, you calculate the current cell using the target_map and the positioner’s global position.
| |
Using the Custom Rule
| |
Example: Rule with Dependencies (PlacementRule)
For rules that don’t need direct tile access, extend PlacementRule directly. Dependencies are typically accessed through the GBCompositionContainer which you can capture during resolve_gb_dependencies or via the _grid_targeting_state’s owner context.
| |
Rule Lifecycle
1. setup(p_gts: GridTargetingState) -> Array[String]
Called to initialize the rule with targeting state. In 5.0.2, this is handled by the IndicatorManager.
| |
2. validate_placement() -> RuleResult
Called during validation to check if placement is allowed. Returns a RuleResult.
| |
3. apply() -> Array[String]
Called after successful placement to perform actions (spend resources, trigger events, etc.). Returns an array of error messages (empty if successful).
| |
4. tear_down() -> void
Called when the rule evaluation cycle is finished or changed.
| |
Validated By
- Rule Logic & Inheritance: res://addons/grid_building/placement/placement_rules/placement_rule.gd — Defines the base contract and lifecycle for 5.0.2 rules.
- Tile-Based Rules: res://addons/grid_building/placement/placement_rules/tile_check_rule.gd and res://addons/grid_building/test/rules/validation/tilemap_bounds_rule_unit_test.gd — Verify coordinate-based validation.
- Rule Orchestration: res://addons/grid_building/test/rules/validation/rule_system_integration_tests.gd — Validates the full sequence of
setup->validate_placement->apply. - Built-in Implementation: res://addons/grid_building/placement/placement_rules/template_rules/spend_materials_rule_generic.gd — Example of a production-stable rule using the
apply()hook.