Expand description
A modular configuration framework for Bevy applications, decoupling configuration access and change detection from management utilities like persistence and UI.
§Getting started
First, decide which Managers you want.
Managers extend the framework with extra capabilities for your config fields,
such as saving and loading from a file (manager::Serde)
or showing a live editor panel in your game (manager::Egui).
Multiple managers can be composed as a tuple; use () if you don’t need any yet.
Declare your chosen managers as a type alias —
you will pass it consistently to every init_config call.
For example, to use both a JSON serializer and an egui editor:
pub type ManagerType = (bevy_mod_config::manager::serde::Json, bevy_mod_config::manager::Egui);Now we can define the configuration data model as required.
Multiple configuration roots are supported,
so each plugin can define its own configuration with #[derive(Config)]:
use bevy_mod_config::Config;
#[derive(Config)]
struct VideoSettings {
width: u32,
height: u32,
orientation: Orientation,
}
#[derive(Config)]
#[config(expose(read))] // it is usually useful to expose the read type for enums
enum Orientation {
Landscape,
Portrait,
}Refer ot the documentation of Config for more customization.
Next, add it to the Bevy app during startup, referencing the ManagerType we just defined:
use bevy_mod_config::AppExt;
struct VideoPlugin;
impl Plugin for VideoPlugin {
fn build(&self, app: &mut App) { app.init_config::<ManagerType, VideoSettings>("video"); }
}Now we can access the configuration data in systems with ReadConfig.
ReadConfig gives us the “read” type of each field instead of the original type,
so we have to match the enum on the read type we exposed earlier:
use bevy_mod_config::ReadConfig;
fn display_system(settings: ReadConfig<VideoSettings>) {
let settings = settings.read();
match settings.orientation {
OrientationRead::Landscape => display_landscape(),
OrientationRead::Portrait => display_portrait(),
}
}Note that ReadConfig must use the same type as the one passed to init_config.
Use ReadConfigChange instead to observe changes.
This change detection is on a per-configuration-root basis.
use bevy_mod_config::ReadConfigChange;
fn resize_system(mut settings: ReadConfigChange<VideoSettings>) {
if settings.consume_change() {
let settings = settings.read();
resize_window(settings.width, settings.height);
}
}Now that we have configuration data defined, we can use managers for persistence, loading and more. See the documentation of each manager module for examples.
§What’s next
- See
Managerfor implementing your own behavior on configuration data. - See
ConfigFieldfor implementing your own field types.
A manager can only be used when all config fields in the app
implement ConfigFieldFor for that manager,
If you are writing a reusable plugin for other crates,
you should accept a generic manager type parameter M:
struct MyPlugin<M>(PhantomData<M>);
impl<M: Manager + Default> Plugin for MyPlugin<M>
where
MyConf: ConfigFieldFor<M>,
{
fn build(&self, app: &mut App) { app.init_config::<M, MyConf>("my_module"); }
}
#[derive(Config)]
struct MyConf {
// ...
}Re-exports§
Modules§
Macros§
- impl_
scalar_ config_ field - Implements
ConfigFieldfor a scalar (non-composite) type.
Structs§
- Child
Node List - Lists the child nodes of a config field node.
- Child
Node Of - Marks an entity as a child node of a config field.
- Conditional
Relevance - If a node entity has this component, it is conditionally “irrelevant” based on the state of another entity.
- Config
Node - Marks an entity as a config field node.
- Enum
Discriminant Metadata - Metadata type for enum discriminants.
- Enum
Discriminant Wrapper - A
ConfigFieldimplementation that wraps anEnumDiscriminantimplementor. - Enum
Field Metadata - Metadata type for fields whose type is a
Config-derived enum. - Field
Generation - Tracks the number of changes to a config field.
- Read
Config - Access to a tree of config fields from a root config type
Cthat was passed intoApp::init_config. - Read
Config Change - Access to a tree of config fields from a root config type
C, and maintains a local state to track changes since the last check. - Root
Node - Marks an entity as a root config node.
- Scalar
Data - Stores the typed data of a scalar config field.
- Scalar
Field - Marks an entity as a scalar config field.
- Scalar
Metadata - Stores the metadata of a scalar config field.
- Spawn
Context - Context information of the config field from its referrers.
- Struct
Metadata - Metadata type for
ConfigFieldimplementors derived fromConfig.
Traits§
- AppExt
- Extension trait for App to initialize config systems.
- Config
Field - Field types that can be used in a
Configstruct/enum. - Config
Field For - Determines how a
ConfigFieldimplementor interacts with aManagertype. - Enum
Discriminant - Implemented by the discriminant type generated by
crate::Configwhen derived for enums. - Query
Like - A
Query-like type that extracts component data from an entity, allowing a large query to be mapped to a subset of its requests. - Spawn
Handle - The spawn handle of a config node.
Functions§
- init_
config_ node - Initializes a newly spawned config node entity with the required components from the context.
Derive Macros§
- Config
- Derives
ConfigFieldon a struct or enum, allowing it to be embedded in anotherConfigFieldstruct/enum or passed intoinit_config.