Crate bevy_2settings

Crate bevy_2settings 

Source
Expand description

§bevy_2settings

Crates.io docs.rs

§Features

What bevy_2settings offers:

  • An easy to setup, macro based settings system.
  • Dynamic range of configurators
    • Want to use multiple? You can!
  • Fully documented, and panic free (minus plugin code, see here.)
  • TODO: UI components, that can be easily bound

§Supported Bevy Versions

Bevybevy_2settings
0.160.0.1

§Getting Started

  1. Add bevy_2settings to your Cargo.toml (bevy_2settings = 0.0.1).
  2. Derive Settings, Resource, Serialize and Reflect on your settings struct.
  3. Add a configurator to your plugin (or, add the SettingsPlugin raw).
use bevy::prelude::*;
use bevy_2settings::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Settings, Resource, Serialize, Debug, Reflect)]
struct Test {
    /// Testing Description!
    ///
    /// NEW LINE!!
    #[settings(name = "Big Screen", default = test_system, predicate = pred)]
    fullscreen: bool,
    #[settings(name = "Nested Test", nested)]
    nested: NestedTest,
}

#[derive(Settings, Serialize, Debug, Reflect)]
struct NestedTest {
    #[settings(name = "No Screen")]
    anti_screen: bool,
    #[settings(name = "Control Selector", default = || bevy_ok(ControlSelector::Keyboard))]
    control_selector: ControlSelector,
}

#[derive(Clone, Serialize, Deserialize, Debug, Reflect)]
enum ControlSelector {
    Keyboard,
    Mouse,
}

fn test_system() -> Result<bool, BevyError> {
    Ok(true)
}

fn pred() -> Result<bool, BevyError> {
    Ok(true)
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(Test::with_configurator(TomlFileConfiguration::default()))
        .add_systems(Update, (get_settings, space_pressed))
        .run();
}

fn get_settings(settings: Res<Test>) {
    println!("{settings:?}");
}

fn space_pressed(keys: Res<ButtonInput<KeyCode>>, mut settings: ResMut<Test>) {
    if keys.just_pressed(KeyCode::Space) {
        settings.nested.anti_screen = !settings.nested.anti_screen;
    }
    if keys.just_pressed(KeyCode::ArrowLeft) {
        settings
            .set_value("nested.control_selector", &ControlSelector::Mouse)
            .unwrap();
    }
    if keys.just_released(KeyCode::ArrowRight) {
        settings
            .set_value("nested.control_selector", &ControlSelector::Keyboard)
            .unwrap();
    }
}

§Macro Details

The macro can be used in the following ways:

#[derive(Settings)]
#[settings(rename = "New Internal Name", styling(...))]
struct Potato {
    #[settings(
        name = "",
        styling(...),
        default = Potatoes,
        default = || bevy_ok(Potatoes),
        default = false, // Allowed, given the field is a literal of some form,
        predicate = false, // Shares what default does.
        nested
    )]
    field: Potatoes
}

struct Potatoes;

Modules§

prelude
The prelude re-export

Structs§

Field
The metadata for a field
Meta
The metadata for the settings
SaveConfigEvent
Event triggered to allow saving a config
SettingsPlugin
The main plugin used for settings

Enums§

Styling
List of stylings

Traits§

DynamicSetting
Trait to allow for dynamic settings
Settings
The main trait for settings

Functions§

bevy_ok
Simple wrapper function for bevy’s Ok