Hazel

Components

Any Rust type can be a Bevy component. This page covers the two Renzora layers on top of a plain one: making it save in scenes (reflection and registration) and making it editable in the Inspector (#[derive(Inspectable)]). It assumes ECS & Bevy.

A plain component

use bevy::prelude::*;

#[derive(Component)]
pub struct Inventory {
    pub items: Vec<String>,
    pub max_slots: usize,
}

This works in systems and queries straight away. It is invisible to scene files and to the editor until it is reflected and registered.

Making it save

A scene stores components through reflection (see Scenes), so a component is written only if its type is reflected and registered in the app's type registry:

use bevy::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Component, Reflect, Serialize, Deserialize, Default, Clone, Debug)]
#[reflect(Component, Default, Serialize, Deserialize)]
pub struct DayNightCycle {
    /// 0.0 is midnight, 0.5 is noon.
    pub time_of_day: f32,
    /// In-game hours per real second.
    pub cycle_speed: f32,
}
PieceWhy
ReflectLets the scene code walk the value at run time
#[reflect(Component)]Registers ReflectComponent, which is how a load inserts the value onto an entity and a save reads it off
Default + #[reflect(Default)]Lets the loader and the Inspector construct one
Serialize, Deserialize + #[reflect(Serialize, Deserialize)]Optional. With them, the value is encoded by your serde impls instead of field by field through reflection

A marker (no fields) needs only #[derive(Component, Reflect, Default)] and #[reflect(Component)].

Register it from a plugin:

#[derive(Default)]
pub struct DayNightPlugin;

impl Plugin for DayNightPlugin {
    fn build(&self, app: &mut App) {
        app.register_type::<DayNightCycle>()
            .add_systems(Update, advance_day_night);
    }
}

fn advance_day_night(time: Res<Time>, mut cycles: Query<&mut DayNightCycle>) {
    for mut cycle in &mut cycles {
        cycle.time_of_day = (cycle.time_of_day + cycle.cycle_speed * time.delta_secs() / 24.0).fract();
    }
}
[[package.metadata.renzora.plugin]]
type = "DayNightPlugin"   # Runtime scope by default

Register from a Runtime plugin. An Editor plugin only exists where the editor image is loaded. A type registered only there is unknown to the shipped game, and the game skips it when it loads the scene.

The rules a save and a load follow, briefly (the full version is on Scenes):

  • Only entities with a Name are saved.
  • Resources are never saved.
  • Derived and runtime-only components (meshes, materials, physics bodies, animation state, render plumbing) are left out and rebuilt on load.
  • A component whose type is unknown, or whose value no longer fits the type, is skipped on load and reported; the rest of the scene loads.
  • A field added after scenes exist needs #[reflect(default)] and #[serde(default)]. A renamed type needs renzora_bsn::register_component_alias(old, new). A moved type needs nothing.

Making it editable in the Inspector

#[derive(Inspectable)] generates an Inspector card from the fields. The derive and AppEditorExt come from the editor contract, renzora_editor_api:

use bevy::prelude::*;
use renzora_editor_api::{AppEditorExt, Inspectable};

#[derive(Component, Reflect, Default, Inspectable)]
#[reflect(Component, Default)]
#[inspectable(name = "Health", icon = "HEART", category = "gameplay")]
pub struct Health {
    #[field(speed = 1.0, min = 0.0, max = 10000.0)]
    pub current: f32,
    #[field(speed = 1.0, min = 1.0, max = 10000.0)]
    pub max: f32,
    #[field(name = "Shield")]
    pub has_shield: bool,
}

app.register_inspectable::<Health>() calls register_type::<Health>() and registers the generated card. The derive adds none of Component, Reflect or Default; list them yourself.

Struct attributes: #[inspectable(...)]

AttributeEffectDefault
nameCard titleThe struct name, title-cased
iconPhosphor icon constant in SCREAMING_SNAKE_CASE (HEART, SNEAKER_MOVE), converted to the kebab-case glyph nameCUBE
categoryThe Inspector page the card is on, also its group in Add Component (see Editor API)object
type_idStable string id for the entryThe struct name in snake_case

Field attributes: #[field(...)]

AttributeEffect
nameThe field's label (default: the field name, title-cased)
speedDrag sensitivity for float and vector widgets
min, maxClamp range for float widgets
skipLeave the field out of the card
readonlyShow the value without letting it be edited

Which widget a field gets

The widget is chosen from the field's Rust type:

Field typeWidget
f32, f64Float drag (speed default 0.01; honours min, max)
boolCheckbox
Vec3Three-component drag (speed default 0.1)
StringText input
ColorColour picker
Anything else, including integersRead-only text

Fields named enabled, and fields whose names start with _p, are skipped: those names are reserved by the post-process effect macro. The derive only accepts structs with named fields. For widgets the derive cannot produce (integers, enums, asset paths), register an InspectorEntry by hand; see Inspector Fields.

Data and editor in one crate

A plugin's scope is either Runtime or Editor, never both, and the editor contract must stay out of a lean game export. Engine crates solve this with an optional dependency behind their own editor feature, on by default and switched off by lean exports:

[features]
default = ["editor"]
editor = ["dep:renzora_editor_api"]

[dependencies]
bevy = { workspace = true }
renzora_editor_api = { path = "../api/renzora_editor_api", optional = true }
impl Plugin for HealthPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(Update, regenerate);

        #[cfg(feature = "editor")]
        app.register_inspectable::<Health>();
        #[cfg(not(feature = "editor"))]
        app.register_type::<Health>();
    }
}

The type is registered in every build, which is what keeps scenes loading in the game; only the card depends on the feature. renzora_ik is a real example. When the editor side is larger than a few registrations, split it into an Editor-scoped plugin in a renzora_<name>/editor subcrate instead, and keep register_type in the runtime crate.

A native plugin writes the same code against renzora: renzora::{AppEditorExt, Inspectable}, with the editor-only registration inside renzora::editor! { ... } so it is compiled out of a lean game build. See Native Plugins.

Components or resources

ComponentResource
Lives onAn entityThe world, one instance
Accessed withQuery<&T>Res<T>, ResMut<T>
Use forPer-entity dataGlobal state and configuration
Saved in scenesYes, if reflected, registered and on a named entityNo

Resources are covered on Resources & State.