This guide explains how the targeting system works in 5.0.2, including the relationship between TargetingSystem, GridTargetingSystem, GridPositioner2D, and GridTargetingState.
Core Targeting Components
| Component | Purpose |
|---|---|
| TargetingSystem | Owns input processing and determines what the player is targeting |
| GridTargetingSystem | Processes grid-specific targeting logic |
| TargetingShapeCast2D | ShapeCast2D used to query objects under the cursor |
| GridPositioner2D | Node2D positioning on the grid (separate from ShapeCast) |
| GridTargetingState | Holds the current targeting state (target cell, object, etc.) |
Input Ownership
TargetingSystem owns all input processing for the building system. It:
- Receives input events (mouse movement, clicks)
- Determines the target position
- Updates GridTargetingState with results
| |
The GridTargetingSystem then processes this targeting data to determine:
- Which grid cell is targeted
- What object (if any) is at that position
- Whether placement is valid
TargetingShapeCast2D: ShapeCast2D
TargetingShapeCast2D is a ShapeCast2D node that queries for objects under the cursor:
| |
Configuration
| |
GridPositioner2D: Node Positioning
GridPositioner2D is a separate Node2D component used for grid positioning. It is not the ShapeCast - that’s handled by TargetingShapeCast2D.
GridTargetingState
The GridTargetingState holds all targeting information:
| |
Processing Flow
Input Event (Mouse)
โ
GridTargetingSystem processes input
โ
TargetingShapeCast2D queries scene for colliders
โ
GridPositioner2D positions preview node on grid
โ
GridTargetingState updated
โ
Preview/Indicator system reacts to state
Targeting in 2D
The 2D targeting system uses:
- World-to-Grid conversion: Converts mouse position to grid coordinates
- ShapeCast queries: Finds objects at the target position
- Grid cell evaluation: Determines what’s on each cell (tilemap, objects, etc.)
| |
Preview Stability
Preview stability depends on GridTargetingState remaining consistent. Common issues:
| Issue | Cause | Solution |
|---|---|---|
| Preview flickers | Rapid state changes | Debounce targeting updates |
| Preview disappears | Invalid target computed | Check is_valid flag |
| Preview at wrong position | Grid/world conversion error | Verify cell_size matches |
Debugging Targeting
| |
Cross Test Validation
Targeting behavior is cross-validated across unit and integration suites to ensure the same targeting state and collision semantics in isolated and scene-driven tests.
- Unit validation: res://addons/grid_building/test/building/placement/rect4x2_bounds_validation_unit_test.gd (bounds validation)
- Validation semantics: res://addons/grid_building/test/building/placement/drag_manager_unit_test.gd (targeting updates)
- Scene-driven verification: environment and workflow guides under
v5-0/guides/
Related Guides
Validated By
The targeting system’s behavior and integration are validated by the following test suites:
- Targeting Logic: res://addons/grid_building/test/unit/grid_targeting_system_test.gd โ Core unit tests for targeting state updates and math utils.
- Physics Queries: res://addons/grid_building/test/positioning/targeting/targeting_shape_cast_2d_unit_test.gd โ Validates
TargetingShapeCast2Dcollision detection and target promotion logic. - Bounds Validation: res://addons/grid_building/test/building/placement/rect4x2_bounds_validation_unit_test.gd โ Validates grid bounds checking and cell position calculations.
- Integration: res://addons/grid_building/test/positioning/targeting/targeting_integration_test.gd โ Full integration tests for targeting within a scene environment.