bevy_config_file
A simple Bevy plugin for loading configuration from YAML, JSON, or RON files with environment variable overrides.
Features
- Load configuration from YAML, JSON, or RON files at application startup
- Format is detected automatically from the file extension
- Override configuration values using environment variables (always JSON)
- Automatic resource registration with Bevy's reflection system
- Type-safe configuration with serde deserialization
- Support for any type that implements
Resource,Deserialize,Serialize, andReflect
Installation
Add this to your Cargo.toml:
[]
= "0.2"
This enables the default features (yaml and logging). To use other formats, enable the corresponding features:
# YAML + JSON
= { = "0.2", = ["yaml", "json"] }
# All formats
= { = "0.2", = ["yaml", "json", "ron"] }
# JSON only, no logging
= { = "0.2", = false, = ["json"] }
Cargo Features
| Feature | Default | Description |
|---|---|---|
yaml |
yes | YAML config support (.yaml, .yml) |
json |
no | JSON config support (.json) |
ron |
no | RON config support (.ron) |
logging |
yes | Log config loading events |
At least one format feature must be enabled.
Quick Start
- Define your configuration struct:
use *;
use ;
use ;
- Create your config file (format is detected from the file extension):
YAML (camera_settings.yaml):
pan_speed: 1000.0
zoom_speed: 1.0
initial_height: 1000.0
JSON (camera_settings.json):
RON (camera_settings.ron):
(
pan_speed: 1000.0,
zoom_speed: 1.0,
initial_height: 1000.0,
)
- Add the plugin to your Bevy app:
- Access the configuration in your systems:
Environment Variable Overrides
You can override configuration values at runtime using environment variables. This is particularly useful for testing or debugging.
The environment variable name follows the pattern CONFIG_{TypeName} where TypeName is the last component of your type's fully qualified name.
Note: Overrides are always JSON, regardless of the config file format.
Example
For a type my_game::config::CameraSettings, you would use:
CONFIG_CameraSettings='{"pan_speed": 2000.0}'
The JSON object should contain the fields you want to override. Only top-level fields are overridden; nested objects are replaced entirely, not merged.
Testing Use Case
This feature is especially useful in tests:
Advanced Usage
Manual Loading
If you need more control over when and how the configuration is loaded, you can use the lower-level functions:
use ;
// In a Bevy system:
// Or load without inserting into ECS:
let config = .expect;
Multiple Configuration Files
You can load multiple configuration files by creating multiple types and adding multiple plugins:
new
.add_plugins
.add_plugins
.add_plugins
.run;