bevy_gauge
bevy_gauge is a flexible stat and modifier system for the Bevy game engine, designed to manage complex character statistics, buffs, debuffs, and equipment effects with ease.
Core Features
- Dynamic Stats: Define stats like Life, Mana, Strength, etc.
- Modifiers: Add flat bonuses, percentage increases, or complex calculations via expressions.
- Expression Engine: Use mathematical expressions to define how stats are calculated (e.g.,
base * (1 + increased) * more). - Dependencies: Stats can depend on other stats, even across different entities (Sources).
- Tagging: Apply tags (e.g., "Fire", "Physical", "Sword") to stats and modifiers for fine-grained control over effects.
- Caching: Automatic caching of evaluated stats and smart cache invalidation.
- Change Detection:
StatsProxycomponent provides efficient change detection without ownership conflicts. - Derived Components: Easily create Bevy components whose fields are derived from entity stats, with optional write-back functionality.
Quick Start
1. Add to your Cargo.toml
[]
= "0.1" # Replace with the latest version
2. Add the Plugin
use *;
use *;
// Define your game's stat configuration
;
3. Spawning an Entity with Stats
Use the stats! macro to easily initialize stats. StatsInitializer (which stats! creates) automatically adds the Stats component if it's not present.
4. Modifying Stats & Adding/Removing Modifiers
Use the StatsMutator SystemParam for changes.
Note on "Adding/Removing Stats": When a modifier is added for a stat path (e.g., "NewStat" or "NewStat.part") that the entity doesn't yet have, bevy_gauge will create that stat on the fly for the entity. The new stat will use default configurations (e.g., Modifiable type, default total expression "0") unless specific configurations for "NewStat" have been registered using Konfig static methods.
TODO Change to allow users to define their own custom defaults for different stat types
5. Stat Derived Components
Create Bevy components whose fields are automatically updated from stats.
Define your component and implement StatDerived (and optionally WriteBack):
The component update systems (included in bevy_gauge::plugin) will automatically call update_from_stats on your Life component when its underlying stats change, detected via the StatsProxy system.
You can also use the stat_component! macro to more easily define your stat-derived components!
stat_component!;
// Example with Option<f32> fields - these work seamlessly in any position
stat_component!;
// Example starting with Option<f32> fields - this now works correctly
stat_component!;
// For more complex examples:
stat_component!;
// Nested structures work too:
stat_component!;
Field Type Support: The macro supports various field types:
f32: Standard floating-point stat valuesOption<f32>: Optional stats where0.0values are converted toNone, andNonevalues write back as0.0String,bool, and other types: Non-stat fields that maintain their values but aren't derived from statsModifierSetand other complex types: For advanced use cases
The $ syntax automatically generates stat paths based on your component's structure:
field: f32 <- $becomesfield: f32 <- "$[StructName.field]"- For nested fields:
nested.field: f32 <- $becomes"$[StructName.nested.field]" - You can mix explicit paths and auto-generated ones as needed
TODO explain why this (2 sources of truth) can be done safely
Change Detection with StatsProxy
bevy_gauge includes a StatsProxy component that automatically tracks when an entity's Stats have been modified. This provides efficient change detection without the ownership conflicts that would occur if you tried to use Changed<Stats> directly in systems that also use StatsMutator.
How It Works
- Automatic Tracking: The
update_stats_proxy_systemruns at the end of theStatsMutationschedule - Change Detection: It uses
Changed<Stats>to detect when stats have been modified - Proxy Updates: Updates the
StatsProxycomponent to trigger Bevy's change detection - Efficient Processing: Your systems can use
Changed<StatsProxy>to process only entities whose stats actually changed
Example Usage
use *;
use *;
// System that only processes entities with changed stats
The StatsProxy system is automatically included when you add bevy_gauge::plugin to your app.
Dive Deeper
For more advanced features like Sources, Tags, Stat Effects, and detailed explanations, please refer to the User Guide.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request.
Version Compatibility
| bevy_gauge_macros | bevy_gauge | bevy |
|---|---|---|
| 0.1 | 0.1 | 0.16 |
License
bevy_gauge is dual-licensed under either
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) at your option.
TODO
Implement string interning.