Hazel

ECS & Bevy

Renzora is built on Bevy's Entity Component System, from Bevy main (0.20-dev) carried in the repository's bevy/ directory. Engine crates, native plugins and Rust scripts all use the same Bevy, linked once as a shared bevy_dylib, so everything on this page is stock Bevy and applies unchanged everywhere. The last section covers what Renzora layers on top.

The four pieces

PieceWhat it is
EntityAn Entity id. It holds no data itself
ComponentA Rust type attached to an entity. Components say what an entity is
SystemA function that queries entities by their components. Systems say what happens
ResourceA single global value of a type, not attached to any entity

Bevy stores components of the same set together, which keeps iteration cache-friendly, and runs systems in parallel wherever their data access does not conflict.

Entities and components

use bevy::prelude::*;

#[derive(Component)]
struct Player;

#[derive(Component)]
struct Health {
    current: f32,
    max: f32,
}

fn spawn_player(mut commands: Commands) {
    commands.spawn((
        Name::new("Player"),
        Transform::default(),
        Player,
        Health { current: 100.0, max: 100.0 },
    ));
}

Insert and remove components through Commands:

commands.entity(entity).insert(Poisoned { seconds: 5.0 });
commands.entity(entity).remove::<Poisoned>();

despawn() removes the entity and every entity related to it as a child; there is no separate recursive call. try_despawn() does the same without warning when the entity is already gone.

Hierarchy is the ChildOf(parent) relationship component, with Children maintained on the parent automatically. Insert ChildOf to parent an entity; read Children to walk down.

Queries

Query<(&Transform, &Health)>                  // read two components
Query<&mut Health, With<Player>>              // write, filtered by a marker
Query<&Health, Without<Player>>               // exclude a marker
Query<&Health, Changed<Health>>               // changed since this system last ran
Query<&Health, Added<Health>>                 // added since this system last ran
Query<(Entity, Option<&Name>)>                // optional component

query.get(entity) and get_mut return a Result; single() and single_mut() return a Result for a query expected to match exactly one entity, rather than panicking.

Systems

A system is a function whose parameters all implement SystemParam:

fn regenerate(time: Res<Time>, mut query: Query<&mut Health, With<Player>>) {
    for mut health in &mut query {
        health.current = (health.current + 5.0 * time.delta_secs()).min(health.max);
    }
}

Systems are added from a plugin's build. Scheduling, ordering, run conditions and exclusive systems are on Systems.

Resources

#[derive(Resource, Default)]
struct Score(u32);

fn add_points(mut score: ResMut<Score>) {
    score.0 += 10;
}

// in build():
app.init_resource::<Score>();

Option<Res<T>> reads a resource that may not exist. More on Resources & State.

Messages and observers

Bevy has two mechanisms for one-shot communication, and they are not interchangeable.

Messages: buffered, polled

A message is written into a buffer and read by systems that poll it, later in the frame or the next. Derive Message, register it, write with MessageWriter::write, read with MessageReader::read:

#[derive(Message)]
struct EnemyDied {
    xp: u32,
}

fn award_xp(mut deaths: MessageReader<EnemyDied>, mut score: ResMut<Score>) {
    for death in deaths.read() {
        score.0 += death.xp;
    }
}

// in build():
app.add_message::<EnemyDied>();

Reading a message type nobody registered fails at the first frame with a missing resource. When a plugin reads a message that another, optional plugin owns, register it in both: add_message is idempotent. Renzora's Rust-script plugin does exactly this for ProjectFileChanged, because the watcher that normally registers it is absent in an exported game.

Observers: immediate

An observer runs the moment an event is triggered. Derive Event, add an observer, and trigger from Commands or &mut World:

#[derive(Event)]
struct Explosion {
    radius: f32,
}

fn on_explosion(trigger: On<Explosion>) {
    info!("boom, radius {}", trigger.event().radius);
}

// in build():
app.add_observer(on_explosion);

// anywhere with Commands:
commands.trigger(Explosion { radius: 5.0 });

Component lifecycle events are observers too. The component goes inside the event pattern, and the entity is a field of the event:

fn on_health_inserted(trigger: On<Insert<Health>>, query: Query<&Health>) {
    let entity = trigger.entity;
    if let Ok(health) = query.get(entity) {
        info!("{entity} has {} HP", health.current);
    }
}

The patterns are Add<B> (first time a component appears), Insert<B> (every insert), Discard<B> (before a value is replaced or removed), Remove<B> and Despawn<B>. A bundle in the pattern is an OR: Insert<(A, B)> fires for either.

Renzora uses observers for its event-style contracts: ScriptAction, the scene SceneLoaded / SceneLoadFailed events, and the editor's request events are all triggered rather than written as messages.

Schedules

ScheduleWhen
PreStartup, Startup, PostStartupOnce, before the first frame
First, PreUpdate, Update, PostUpdate, LastEvery frame, in that order
FixedUpdateAt a fixed rate (64 Hz by default), zero or more times per frame

Inside FixedUpdate, Res<Time> reports the fixed clock, so delta_secs() is the fixed step.

How Renzora builds on Bevy

One binary, two sessions. The renzora executable is the game. When renzora_editor.<dll|so|dylib> sits beside it, it loads that image and becomes the editor; delete the file and the same binary is the shipped game. renzora_runtime::add_engine_plugins(app, is_editor) inserts EditorSession(is_editor) first, then an ordered foundation (RuntimePlugin, InputPlugin, and when compiled in, ScriptingPlugin and PhysicsPlugin; ViewportStretchPlugin and RenderScalePlugin only in a game), then every other runtime plugin from the generated list.

Plugins are declared, not hand-wired. Every engine feature is a Bevy Plugin in its own crate, declared in that crate's Cargo.toml:

[[package.metadata.renzora.plugin]]
type = "MyPlugin"   # path from the crate root
scope = "Runtime"   # or "Editor"; Runtime is the default
priority = 0        # optional; lower builds earlier

cargo renzora sync reads every table and writes the committed lists crates/renzora_runtime/src/plugins.rs and crates/renzora_editor/src/plugins.rs. CI fails if regenerating them changes anything. A plugin's type must implement Plugin + Default. See Plugins.

Scope is exclusive. A Runtime plugin runs in the editor viewport and in the shipped game; an Editor plugin only where the editor image is loaded. There is no "both": a feature that needs editor tooling ships two plugins, usually a renzora_<name> crate and a renzora_<name>/editor subcrate.

Shared types live in the contract. Any type two crates both need has one definition, in a crates/api/renzora_*_api layer (or the bottom layer, crates/shared/renzora), and those layers are linked into one shared image, renzora_dylib. That is what makes a Res<CurrentProject> in the engine and in a native plugin the same resource. See Resources & State.

Imports. Bevy's types come from use bevy::prelude::*;. There is no renzora::prelude: name contract items by path (use renzora::ScriptCtx;) or glob the crate root.

No egui. The editor UI is bevy_ui plus Renzora's own framework, renzora_ember.