Hazel

Systems

Systems are plain Bevy systems, added from a plugin's build. This page covers the Bevy scheduling tools you will use, and the few Renzora rules that decide in which session a system runs and in what order plugins are built. It assumes ECS & Bevy.

Systems live in plugins

use bevy::prelude::*;

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

#[derive(Default)]
pub struct ScorePlugin;

impl Plugin for ScorePlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<Score>()
            .add_systems(Update, tick_score);
    }
}

fn tick_score(mut score: ResMut<Score>) {
    score.0 += 1;
}
# The crate's Cargo.toml
[[package.metadata.renzora.plugin]]
type = "ScorePlugin"

The table is read at build time by cargo renzora sync, which writes the plugin into a committed, generated list; nothing registers it at run time. The type must implement Plugin + Default; a wrong name or a missing Default is a compile error in the generated plugins.rs. See Plugins.

Runtime or Editor scope

scope decides which session a plugin exists in:

ScopeRuns inUse for
Runtime (default)The editor's viewport and the shipped gameGameplay, rendering, physics, audio, UI: anything the game needs
EditorOnly where the editor image is loadedPanels, Inspector cards, gizmos, authoring tools

The match is exact; there is no "both". A feature that needs runtime behaviour and editor tooling ships two plugins, one of each scope. Anything the game depends on must be Runtime: an Editor system works while you author and is simply absent from the export.

A Runtime plugin also runs while you edit, so its systems need to decide whether they should act in edit mode. The usual gates:

ConditionWhereTrue when
renzora_scripting::scripts_should_runrenzora_scriptingScripts are running: a game process, Play, or a script preview
not_in_play_moderenzora_project_api (renzora::not_in_play_mode)No PlayModeState, or it is Editing
EditorSession::is_editor()renzora_project_apiRead inside a system: this process is the editor

Play starts the game as a separate process, so most gameplay simply runs in that process and never needs a gate. The gates matter for systems that must stay quiet in the editor's own world.

When plugins are built

renzora_runtime::add_engine_plugins(app, is_editor) builds the runtime side of every session:

  1. Inserts EditorSession(is_editor), so every plugin after it can read which session it is in.
  2. Builds an ordered foundation: RuntimePlugin (virtual file system, asset reader, scene I/O, autoload), InputPlugin, then ScriptingPlugin and PhysicsPlugin when those features are compiled in. In a game only, ViewportStretchPlugin and RenderScalePlugin.
  3. Builds every other Runtime plugin from the generated list, in ascending priority (default 0), then by type path.

Editor plugins come from the editor image's own generated list, built on top when that image is present. Native plugins are built while the App is being assembled too, so their Startup systems run like anyone else's.

priority orders build calls only. It says nothing about when systems run each frame. Use it in the rare case where one plugin's build must see a resource another plugin's build inserts; order systems with Bevy's tools below.

Signatures

A system is a function whose parameters all implement SystemParam:

fn my_system(
    time: Res<Time>,                        // read a resource
    mut score: ResMut<Score>,               // write a resource
    query: Query<(&Transform, &Health)>,    // read entity data
    mut commands: Commands,                 // deferred spawn, insert, remove, despawn
    mut deaths: MessageWriter<EnemyDied>,   // write buffered messages
    mut spawns: MessageReader<SpawnRequest>,// read buffered messages
    mut counter: Local<u32>,                // private state that persists between runs
) {
    // ...
}

Commands are applied at the next sync point, not immediately; a spawn is not visible to a query in the same system.

Schedules

ScheduleWhen
PreStartup, Startup, PostStartupOnce, before the first frame
First, PreUpdateStart of every frame
UpdateEvery frame: most game logic
PostUpdate, LastAfter Update: transform propagation, render preparation
FixedUpdateAt a fixed rate, zero or more times per frame

Ordering

Bevy runs systems in parallel whenever their data access does not conflict. Constrain order only where it matters:

app.add_systems(Update, (gather_input, move_player, apply_damage).chain());
app.add_systems(Update, cleanup.before(spawn_enemies));
app.add_systems(Update, follow_camera.after(move_player));

Sets

A system set orders many systems at once and is the right tool across plugins: a plugin exposes a public set, and other plugins put their systems before, after or in it without naming individual functions.

#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
enum GameSet {
    Input,
    Logic,
}

app.configure_sets(Update, (GameSet::Input, GameSet::Logic).chain())
    .add_systems(Update, gather_input.in_set(GameSet::Input))
    .add_systems(Update, move_player.in_set(GameSet::Logic));

Engine sets worth ordering against:

SetCrateMeaning
ScriptingSet::PreScript, ScriptExecution, CommandProcessing, DebugDraw, Cleanuprenzora_scriptingScript frame order; see The Scripting System
PhysicsSet::Read, PhysicsSet::Commandsrenzora_physics_apiReading results after a step; queuing forces before one

Run conditions

#[derive(Resource, PartialEq)]
enum Phase {
    Playing,
    Paused,
}

app.add_systems(Update, pause_menu.run_if(resource_equals(Phase::Paused)))
    .add_systems(Update, spawn_waves.run_if(is_playing));

fn is_playing(phase: Res<Phase>) -> bool {
    *phase == Phase::Playing
}

Bevy's built-in conditions include resource_exists::<T>, resource_changed::<T>, resource_equals(value), any_with_component::<T>, run_once, in_state(state) and on_timer(duration). Combine them with not(..) and the combinator methods: a.and_then(b) and a.or_else(b) short-circuit (b is not evaluated when the answer is already known); a.and_eager(b) and a.or_eager(b) always evaluate both; xor, nand_then and friends exist as well. There is no .and or .or.

A run condition is evaluated per system every frame. When several systems share an expensive test, compute it once into a resource and gate on that, the way scripting computes ScriptsActive.

Fixed timestep

app.add_systems(FixedUpdate, physics_step)
    .insert_resource(Time::<Fixed>::from_hz(60.0)); // default is 64 Hz

Inside FixedUpdate, Res<Time> is the fixed clock, so delta_secs() is the tick length. On a fast frame FixedUpdate may not run at all, and on a slow one several times; input edges (just_pressed) can therefore be seen twice or missed there, so read them in Update.

One-shot systems

Register a system to run on demand:

#[derive(Resource)]
struct SpawnWave(bevy::ecs::system::SystemId);

fn setup(world: &mut World) {
    let id = world.register_system(spawn_wave);
    world.insert_resource(SpawnWave(id));
}

fn spawn_wave(mut commands: Commands) {
    commands.spawn((Name::new("enemy"), Transform::default()));
}

fn on_wave_timer(mut commands: Commands, wave: Res<SpawnWave>) {
    commands.run_system(wave.0);
}

Keep the SystemId somewhere a later system can reach it, such as a resource.

Exclusive systems

A system that takes &mut World runs alone, with nothing in parallel:

fn reset_all_health(world: &mut World) {
    let mut query = world.query::<&mut Health>();
    for mut health in query.iter_mut(world) {
        health.current = health.max;
    }
}

It serialises the schedule around itself, so use one only for work that genuinely needs the whole world at once (scene loading, the script dispatcher).

Example: despawn after a timer

#[derive(Component)]
struct DespawnTimer(Timer);

fn tick_despawn_timers(
    time: Res<Time>,
    mut commands: Commands,
    mut query: Query<(Entity, &mut DespawnTimer)>,
) {
    for (entity, mut timer) in &mut query {
        timer.0.tick(time.delta());
        if timer.0.is_finished() {
            commands.entity(entity).despawn(); // children go too
        }
    }
}

Keeping systems cheap

The editor and the game run every Runtime system every frame, so idle cost adds up:

  • Filter with Changed<T>, Added<T>, With<T> and Without<T> rather than fetching and testing in the loop.
  • Gate whole systems with run conditions so they are skipped, not entered and returned from.
  • Do not iterate one query inside another; build a lookup first.
  • Do not log every frame. A line per frame stalls frames on its own.
  • Use FixedUpdate only for work that needs a fixed rate; it can run several times a frame.