Repository navigation
Building Games
A game is a crate in games/. The editor loads it as a library, plays it, and hot-reloads it. A binary in the same crate is what ships. Chapter 3 of the user guide is the walked version of this page. cargo xtask guide runs its checked commands.
cargo xtask new-game my-game --title "My Game"
cargo test -p my-gameThat writes games/my-game/: Cargo.toml, src/lib.rs, src/main.rs, assets/scenes/main.ron, the engine's starting assets (including the options screen and the lobby screen), build.ron, and tests. One test loads the library the way the editor does. Another walks the lobby screen from host to start.
The library is a cdylib (what the editor loads) and an rlib (what the binary links).
src/lib.rs holds one value that implements fanalor_core::Game:
#[derive(Default)]
pub struct MyGame;
impl Game for MyGame {
fn register(&self, registry: &mut ComponentRegistry) {
registry.register::<Player>("Player").with_template();
}
fn fixed_update(&mut self, tick: &mut Tick<'_>) {
let direction = Vec2::new(
tick.input.action_axis("move_left", "move_right"),
tick.input.action_axis("move_down", "move_up"),
);
for (_, transform) in tick.world.query_mut::<Transform>() {
transform.translation += direction * 3.0 * tick.delta;
}
}
}
fanalor_core::export_game!(MyGame);-
registernames the game's components so scenes can store them and the inspector can add and edit them. A component is anySerialize + Deserializestruct. State that must survive a reload, such as a score, lives on one..replicated()marks a component for Networking. -
startruns once, when play begins. -
updateruns once a frame. Input edges (pressed()) are visible once here. UI and text belong here. -
fixed_updateruns once per physics step, withtick.deltaalways the fixed step. Velocities, forces, and collision events belong here.
Tick is everything a turn may touch: world (query, spawn, despawn), input and actions (the map from assets/default.input.ron, including rebinding), events (collisions; each event is handed over once), contacts (what the solver resolved this step; sensors are not in here), audio, ui, tween / after / every, locale, saves, window (fullscreen, size, vsync), net, and scene changes.
Tick::broken_joints reports a joint that broke this step, once. Tick::gravity is how a game moves gravity. Queries do not modify the world:
let hit = query::ray(tick.world, foot, Vec2::NEG_Y, 0.7, QueryFilter::new().excluding(player));
let inside = query::overlap_aabb(tick.world, min, max, QueryFilter::new());A ray that starts inside a shape hits at distance zero. Queries skip sensors unless asked.
Without a game crate, fanalor-core's App takes the same two hooks:
App::new(config)
.with_simulation()
.with_update(|tick| { /* once per frame */ })
.with_fixed_update(|tick| { /* once per physics step */ })
.run();cargo build -p fanalor-editor -p my-game
cargo run -p fanalor-editor -- --game games/my-game/build.ron scenes/main.ron
cargo run -p my-gameThe editor and the game must be built together. The editor checks that the library was built by the same compiler, engine, and settings, and refuses it otherwise.
Leave the editor playing, change a number in src/lib.rs, and save. The crate is rebuilt and swapped in mid-play. The scene is carried across. A failed build is reported in the Game menu; the previous code keeps running.
- An options screen: display, audio, and rebinding. Rebound controls are saved on every platform, including the web.
- A lobby screen prefab,
assets/prefabs/lobby.prefab.ron. See Networking. -
assets/default.input.ronfor named actions (keys, pads, touch). -
build.ron, which Export packages.
games/platformer/tests/level.rs and tests/plugin.rs in the new crate show headless play: fanalor_core::Headless runs a scene without a window, steps it, presses actions, and reads the world.
| Package | Run | What it shows |
|---|---|---|
fanalor-platformer |
cargo run -p fanalor-platformer |
A walker, coins, crates to push, a goal. A and D walk, Space jumps |
breakout |
cargo run -p breakout |
Physics and collision events |
dodge |
cargo run -p dodge |
Timers, tweens, and sensors |
arena |
cargo run -p arena |
Two to four players, a lobby, then a match. Native and web |
The platformer in the editor:
cargo build -p fanalor-editor -p fanalor-platformer
cargo run -p fanalor-editor -- --game platformer.build.ron scenes/platformer.rongames/platformer's walker is a CharacterBody2D that pushes crates and climbs a hill. The character example blends idle, walk, and run by speed, with a landing squash on an additive animation layer.
Further chapters of the guide, all under docs/guide:
| Chapter | Subject |
|---|---|
| 4 | Bodies, colliders, characters that push and climb, tile shapes, joints |
| 5 | HUD, controls, the window, options, text, touch |
| 6 | Sources, buses, streamed music, effects |
| 7 | Native and web export |
| 8 | Networking |
| 9 | Particles, trails, state machines, blend trees, layers, bones |
| 10 | Regions streamed around the player, loading screens |