In these mods, Health, Regen, Score, Hit, Scored and Time are types from the game. The game makes components and resources available with app.expose_to_mods::<T>(), and message types with app.expose_message_to_mods::<M>(). In the demo game, Health has current and max fields; in the test game it’s a tuple struct, Health(f32), so those mods write hp.0.
regen.ox: the demo
Heals every entity that has both Health and Regen, scaled by the frame time and capped at max. The query only matches entities with both components, so the demo’s statue, which has no Regen, isn’t touched. With --features hot-reload, changing the mod while the demo runs takes effect within a frame.
// Regenerates health over time. `Health`, `Regen` and `Time` come from the
// game; edit the rate and run the demo again (or use `--features hot-reload`
// and edit it while the demo runs).
fn regen(time: Res<Time>, mut q: Query<(&mut Health, &Regen)>) {
for (mut hp, regen) in &mut q {
if hp.current < hp.max {
hp.current += regen.per_sec * time.delta_secs();
if hp.current > hp.max {
hp.current = hp.max;
}
}
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, regen);
}crates/oxeon/examples/assets/mods/regen.ox on GitHub
commands.ox: spawning and despawning
Commands spawns entities, from one component or a tuple, and .insert(..) adds components to them. Queries take filters such as Without<Regen>, and Entity in a query gives each entity’s ID, which commands.entity(entity).despawn() uses. Commands are applied after the system returns, as in Bevy.
// Spawns, damages and despawns entities: `Commands`, query filters and
// `Entity`.
fn spawn_wave(mut commands: Commands) {
commands.spawn((Health(3.0), Regen { per_sec: 0.0 }));
commands.spawn(Health(1.0)).insert(Regen { per_sec: 5.0 });
commands.spawn(Health(2.0));
}
// Everything that doesn't regenerate loses health.
fn poison(mut q: Query<&mut Health, Without<Regen>>) {
for mut hp in &mut q {
hp.0 -= 1.0;
}
}
fn reap(mut commands: Commands, q: Query<(Entity, &Health)>) {
for (entity, hp) in &q {
if hp.0 <= 0.0 {
commands.entity(entity).despawn();
}
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Startup, spawn_wave);
app.add_systems(Update, (poison, reap));
}crates/oxeon/tests/mods/commands.ox on GitHub
changes.ox: resources and change detection
ResMut<Score> changes a game resource, and Res<Score> reads one. Changed<Health> and Added<Health> filters match only entities whose component changed or was added since the system last ran. Oxeon only writes back values that actually changed, so a system that takes &mut Health without changing it doesn’t trigger Changed.
// Counts changes into a game resource: `ResMut<T>` and `Changed` / `Added`.
fn touch(mut q: Query<&mut Health, With<Regen>>) {
for mut hp in &mut q {
hp.0 += 1.0;
}
}
fn count_changed(mut score: ResMut<Score>, q: Query<&Health, Changed<Health>>) {
for _hp in &q {
score.changed += 1;
}
}
fn count_added(mut score: ResMut<Score>, q: Query<&Health, Added<Health>>) {
for _hp in &q {
score.added += 1;
}
}
fn read_only(score: Res<Score>, mut q: Query<&mut Regen>) {
for mut r in &mut q {
r.per_sec = score.added as f32;
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, (touch, count_changed, count_added, read_only));
}crates/oxeon/tests/mods/changes.ox on GitHub
messages.ox: messages
MessageReader<Hit> reads the game’s messages, and MessageWriter<Scored> sends one back. Killed is a message type the mod declares itself, to pass kills from one of its systems to another. Mods that declare the same message type the same way share it.
// Reads the game's `Hit` messages, and tells another system about kills
// with a message type of its own; that one reports back to the game.
struct Killed {
points: u32,
}
fn take_hits(mut hits: MessageReader<Hit>, mut q: Query<&mut Health>, mut killed: MessageWriter<Killed>) {
for hit in hits.read() {
for mut hp in &mut q {
let was_alive = hp.0 > 0.0;
hp.0 -= hit.damage;
if was_alive && hp.0 <= 0.0 {
killed.write(Killed { points: 10 });
}
}
}
}
fn score(mut kills: MessageReader<Killed>, mut score: ResMut<Score>, mut out: MessageWriter<Scored>) {
for kill in kills.read() {
score.added += kill.points;
out.write(Scored { total: score.added });
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, (take_hits, score));
}crates/oxeon/tests/mods/messages.ox on GitHub
poison.ox: a component the mod defines
Poisoned is declared in the mod with #[derive(Component)]. Oxeon registers it as a dynamic Bevy component named after the mod, so it lives in the game’s world like any other component, can be inserted with Commands and queried, and survives a hot reload as long as its fields don’t change. The query in infect combines filters in a tuple: (With<Health>, Without<Poisoned>).
// A component the mod defines itself, stored in the game's world.
#[derive(Component)]
struct Poisoned {
per_frame: f32,
frames: u32,
}
fn infect(mut commands: Commands, q: Query<Entity, (With<Health>, Without<Poisoned>)>) {
for entity in &q {
commands.entity(entity).insert(Poisoned { per_frame: 1.0, frames: 3 });
}
}
fn tick(mut q: Query<(&mut Health, &mut Poisoned)>) {
for (mut hp, mut poison) in &mut q {
if poison.frames > 0 {
hp.0 -= poison.per_frame;
poison.frames -= 1;
}
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, (infect, tick));
}crates/oxeon/tests/mods/poison.ox on GitHub
tiers.ox: enums and match in a system
tier returns an enum whose variants carry data, and the system matches on it. Enums live in the mod’s linear memory, and the compiler passes them between functions by value.
// Heals by health tier: an enum and `match` inside a mod system.
enum Tier {
Low,
Mid(f32),
High { cap: f32 },
}
fn tier(hp: f32) -> Tier {
if hp < 5.0 {
Tier::Low
} else if hp < 50.0 {
Tier::Mid(2.0)
} else {
Tier::High { cap: 100.0 }
}
}
fn heal(mut q: Query<&mut Health>) {
for mut hp in &mut q {
match tier(hp.0) {
Tier::Low => hp.0 = 5.0,
Tier::Mid(factor) => hp.0 *= factor,
Tier::High { cap } => {
if hp.0 > cap {
hp.0 = cap;
}
}
}
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, heal);
}crates/oxeon/tests/mods/tiers.ox on GitHub
option.ox: Option and a closure
Option and its methods, such as map and unwrap_or, come with the language. They’re written in Oxeon in a small prelude that’s compiled with every mod. The closure passed to map captures scale by value.
// Tops health up to 10 using `Option`, its methods and a closure, which come
// with the language.
fn missing(hp: f32) -> Option<f32> {
if hp < 10.0 { Some(10.0 - hp) } else { None }
}
fn top_up(mut q: Query<&mut Health>) {
for mut hp in &mut q {
let scale = 1.0;
hp.0 += missing(hp.0).map(|amount| amount * scale).unwrap_or(0.0);
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, top_up);
}crates/oxeon/tests/mods/option.ox on GitHub
spin.ox: a mod that never returns
The loop never exits. Each call into a mod gets a fuel budget, so this one runs out of fuel and traps. Oxeon disables the mod and writes nothing back from the failed call, and the game keeps running.
// Never returns, so it runs out of fuel.
fn spin(mut q: Query<&mut Health>) {
loop {
for mut hp in &mut q {
hp.0 += 1.0;
}
}
}
pub fn plugin(app: &mut App) {
app.add_systems(Update, spin);
}crates/oxeon/tests/mods/spin.ox on GitHub
broken.ox: a type error
hp.0 += true adds a bool to an f32. The compiler rejects the mod with a rustc-style error that points at the expression, and the mod never runs. During hot reload, a version with errors leaves the previous version running.
fn broken(mut q: Query<&mut Health>) {
for mut hp in &mut q {
hp.0 += true;
}
}