Skip to main content

Crate bevy_config_file

Crate bevy_config_file 

Source
Expand description

A Bevy plugin for loading configuration from YAML, JSON, or RON files with environment variable overrides.

This crate provides a simple way to load configuration files into Bevy resources, with support for runtime overrides via environment variables. This is useful for game settings, input mappings, and other configurable parameters.

§Features

  • Load configuration from YAML, JSON, or RON files at startup
  • Format is detected automatically from the file extension in ConfigFile::PATH
  • Override configuration values using environment variables (always JSON)
  • Automatic resource registration with Bevy’s reflection system
  • Type-safe configuration with serde deserialization

§Cargo Features

FeatureDefaultDescription
yamlyesYAML config support (.yaml, .yml)
jsonnoJSON config support (.json)
ronnoRON config support (.ron)
loggingyesLog config loading events

At least one format feature must be enabled. To use multiple formats:

bevy_config_file = { version = "0.2", features = ["yaml", "json", "ron"] }

§Quick Start

The format is detected from the file extension in ConfigFile::PATH.

#[derive(Resource, Reflect, Debug, Serialize, Deserialize)]
#[reflect(Resource)]
pub struct CameraSettings {
    pub pan_speed: f32,
    pub zoom_speed: f32,
}

impl ConfigFile for CameraSettings {
    const PATH: &'static str = "assets/config/camera_settings.yaml";
}

App::new()
    .add_plugins(config_file_plugin::<CameraSettings>)
    .run();

§Config File Formats

The same struct can be loaded from any supported format by changing the file extension:

YAML (camera_settings.yaml):

pan_speed: 1000.0
zoom_speed: 1.0

JSON (camera_settings.json):

{ "pan_speed": 1000.0, "zoom_speed": 1.0 }

RON (camera_settings.ron):

(pan_speed: 1000.0, zoom_speed: 1.0)

§Environment Variable Overrides

You can override configuration values at runtime using environment variables. Overrides are always JSON, regardless of the config file format:

CONFIG_CameraSettings='{"pan_speed": 2000.0}' ./game

The environment variable name is CONFIG_{TypeName} where TypeName is the last component of the type’s fully qualified name.

Enums§

LoadConfigError
Errors that can occur when loading configuration files.

Traits§

ConfigFile
Trait for types that can be loaded from a configuration file.

Functions§

config_file_plugin
Creates a Bevy plugin that loads a configuration resource from a file at startup.
load_config_file
Loads configuration from a file with optional environment variable overrides.
load_resource_from_config_file
Loads a configuration resource from a file and inserts it into Bevy’s ECS.