Hazel

Resources & State

A resource is Bevy's Resource: one value of a type stored in the World, not on any entity. Renzora adds no resource trait or macro. What it adds is a rule about where shared resource types are defined, a handful of engine resources every plugin can read, and a few facts about what survives a scene load.

The basics

use bevy::prelude::*;

#[derive(Resource, Default)]
pub struct GameState {
    pub score: u32,
    pub level: u32,
}

// in build():
app.init_resource::<GameState>();                        // from Default
app.insert_resource(GameState { score: 0, level: 1 });   // from a value

fn restart(mut commands: Commands) {
    commands.insert_resource(GameState::default());
}

fn show_score(state: Res<GameState>) {
    info!("score = {}", state.score);
}

fn add_score(mut state: ResMut<GameState>) {
    state.score += 10;
}

fn maybe(state: Option<Res<GameState>>) {
    if let Some(state) = state {
        info!("level {}", state.level);
    }
}

Take Option<Res<T>> for anything a lean export might strip, or that only exists in one session; Res<T> on a missing resource fails the system.

Why shared types live in the contract

Renzora runs several separately compiled images in one process: the renzora executable, the editor image renzora_editor.<dll|so|dylib> beside it, and any native plugins. A resource is a single source of truth only if every image means the same type by it. Bevy identifies a type by a TypeId derived from its crate, so a type defined in a crate that two images each compiled privately would still match, but any process-global state that crate holds (a static, a thread-local) would exist twice, and each copy would be written by one side and read by the other.

So the types that cross those boundaries are defined once, in the contract layers under crates/api/ (one renzora_*_api crate per subject) and the bottom layer crates/shared/renzora, and all of them are linked into one shared image, renzora_dylib. The executable, the editor image and every native plugin link that image, so a Res<CurrentProject> in the engine and in a plugin is one resource, and the statics behind the translation table, the Console and the script read handlers exist once. The rule for engine crates: a type two crates need goes in a contract layer, never in one of the two crates. See Architecture.

Engine resources

ResourceDefined inHolds
EditorSession(bool)renzora_project_apiWhether this process is the editor; is_editor()
CurrentProjectrenzora_project_apiThe open project: path and config (the parsed project.toml)
PlayModeStaterenzora_project_apiThe editor's play state and request_* flags
Vfsrenzora_engineThe mounted .rpak archive, if the game runs from one
ScriptEnginerenzora_scriptingThe registered script backends, routed by extension
AssetRegistryrenzora_asset_registryEditor only: an index of every project file's path, kind, size and modification time
ProjectIndexrenzora_platform_apiEditor only: a live list of project files, kept current by the file watcher
SceneLoadStaterenzora_engine::scene_ioPhase and progress of the most recent scene load

Plugins reach the contract ones through renzora (renzora::EditorSession, renzora::CurrentProject, renzora::PlayModeState).

EditorSession

The same binary is the editor or the game depending on whether the editor image is beside it, so dual-mode code cannot branch on a compile-time feature. renzora_runtime::add_engine_plugins inserts the answer before any other plugin builds:

use bevy::prelude::*;
use renzora::EditorSession;

fn only_in_game(session: Res<EditorSession>) {
    if !session.is_editor() {
        // shipped-game path
    }
}

CurrentProject

pub struct CurrentProject {
    pub path: PathBuf,          // project root
    pub config: ProjectConfig,  // project.toml
}

resolve_path("scenes/level1.bsn") joins a project-relative path to the root, main_scene_path() resolves main_scene, make_relative(path) goes the other way, and save_config() writes project.toml back.

ProjectConfig mirrors project.toml: name, version, created_with (the engine version that created the project, never rewritten; None means unknown, not old), main_scene (default scenes/main.bsn), autoload, icon, and sections such as window, viewport, rendering, rendering_2d and audio. The editor-only fields editor_last_scene and editor_open_tabs are stripped from exports.

PlayModeState

state is a PlayState: Editing, Playing, Paused or Simulating. Helpers: is_playing(), is_paused(), is_in_play_mode(), is_scripts_running(). Requests (request_play, request_stop, request_pause, request_simulate) are flags the editor consumes on the next frame. The free function not_in_play_mode is a run condition for systems that should only run while editing.

A game process has no PlayModeState at all. Code that treats "absent" as "running" works in both places, which is how scripting decides whether scripts run.

State across scene loads

A scene load despawns entities, never resources. A resource you insert stays exactly as it was through every level change, which makes a resource the simplest place for run-wide state such as a score or a settings cache.

State that needs entities (a HUD, a music player, a network session) goes in an autoload scene: a scene listed under autoload in project.toml, loaded before main_scene, whose entities are tagged Persistent and skipped by every later load and save. See Scenes.

Browsing resources in the editor

The Resources panel (Debug, Resources) lists every reflected resource in the running world and edits its fields live. It is master/detail: a searchable list, and the selected resource's fields below. Only the selected resource is walked through reflection, so the panel costs nothing for the hundreds it is not showing.

A resource appears once it is reflected:

#[derive(Resource, Reflect, Default)]
#[reflect(Resource)]
struct Difficulty {
    enemy_health: f32,
    spawn_rate: f32,
}

// in build():
app.register_type::<Difficulty>();

A resource without #[reflect(Resource)] is counted (412 (+38 unreflected)) but not listed, because without reflection there is no readable name or field to show. Edits made here are not undoable: undo is per document, and a global poked from a debug panel does not belong in the scene's history.

Process-global state that is not a resource

A few things must be readable before the App exists, or from code that has no World, and so are process-global statics in a contract layer rather than resources. They rely on renzora_dylib to exist once. The warnings feed is one:

use renzora::runtime_warnings::recent_warnings;

for warning in recent_warnings() {
    // warning.level, warning.target, warning.message, warning.age()
}

It holds the most recent 200 (MAX_WARNINGS) WARN and ERROR events from anywhere in the process, captured from the moment logging starts. Others are the localization table, the Console buffer and the script read handlers.

Local and non-send state

Local<T> is per-system state that persists between runs, private to that one system:

fn every_second_frame(mut flip: Local<bool>) {
    *flip = !*flip;
    if *flip {
        // ...
    }
}

A type that cannot leave its thread (a raw OS or device handle) is a non-send resource. Its systems run on the main thread:

// in build():
app.insert_non_send(handle);

fn use_handle(handle: NonSend<MyHandle>) {
    // main thread only
}

Change detection

fn on_score_change(state: Res<GameState>) {
    if state.is_changed() {
        info!("score is now {}", state.score);
    }
}

app.add_systems(Update, on_score_change.run_if(resource_changed::<GameState>));

Gating on resource_changed is how most editor and UI systems avoid doing work every frame. Writing through ResMut marks the resource changed even when the value is the same, so compare before assigning when a resource is read by gated systems.