In GridBuilding 6.0, resource consumption is handled by the Validation & Execution pipeline. This ensures that resources are only consumed when a placement is guaranteed to succeed, preventing “ghost spending” (resources lost on failed placements) or “free builds” (placements without cost).
The Consumption Workflow
The spending logic is encapsulated in the ResourceConsumptionRule. This rule operates in two distinct phases:
- Validation Phase (Can I build?): Checks if the player has enough resources.
- Execution Phase (Did I build?): Deducts the resources from the inventory.
1. Validation Phase
When the user hovers over a tile:
- The
PlacementValidationSystemruns. - It invokes
ResourceConsumptionRule.validate(context). - The rule checks the
ResourceInventoryComponent(or provider) for required items. - If resources are missing, it returns a Failure result with a message (e.g., “Need 5 Wood”), which blocks placement.
2. Execution Phase
When the user clicks to place:
- The
PlacementExecutionSystemconfirms the request is still valid. - It invokes
ResourceConsumptionRule.apply(context). - The rule deducts the items from the inventory.
- The placement is committed.
Data Structures
ResourceStackComponent
Costs are defined using ResourceStackComponent, which pairs a resource definition with a quantity.
| |
ResourceConsumptionRuleSettings
Configures how the rule behaves.
| Setting | Type | Description |
|---|---|---|
consume_on_success | bool | If true, resources are automatically deducted on apply(). |
show_resource_details | bool | If true, validation messages include exact missing amounts. |
Integrating with Your Inventory
The system is agnostic to your specific inventory implementation, provided you adapt it to the ResourceInventoryComponent interface or ensure your provider object implements get_resource_count(resource) and remove_resource(resource, amount).
Setup Example
- Define Costs: Add a
ResourceConsumptionRuleto your placeable entity’s configuration. - Assign Provider: Ensure the
PlacementContextpassed to the rule includes your inventory object as theresource_provider.
| |
Advanced: Custom Providers
You don’t have to use ResourceInventoryComponent. You can use any Object that implements the provider interface.
| |
This flexibility allows you to hook into complex RPG inventory systems, creative mode “infinite” providers, or even debug tools without changing the placement logic.
Performance Warning
Resource validation runs every frame while hovering. Ensure your provider’s get_resource_count method is O(1) or very fast. Do not iterate large arrays or make network calls inside this method. Cache values if necessary.
Common Issues
- Race Conditions: If the player drops an item after validation but before execution (in the same frame), the
apply()phase might fail. The system handles this by re-validating immediately before execution. - Infinite Resources: For creative mode, you can simply disable the rule or use a
CreativeInventoryprovider that always returnstruefor validation.
Test Verification
The localized resource consumption logic is strictly verified by the following E2E test suite:
- Workflow Verification:
res://addons/grid_placement/tests/e2e/test_inventory_resource_consumption_workflow.gd- Validation Check: Confirms
validate()returns success when resources exist and failure when they don’t. - Application Check: Confirms
apply()correctly reduces the inventory count. - Double Dip Prevention: Verifies that subsequent validations fail immediately after resources are consumed, preventing negative inventory states.
- Validation Check: Confirms