Pre-1.0: the API is not stable yet and may change from version to version. Pin the exact version in your Cargo.toml and check the changelog before upgrading.

10. Randomness and background tasks

Lots of things happening on their own, while the main story continues — and still the same video every time.

Loading live preview…

Source of learn_10_random — edit in the playground
//! Lesson 10 — Randomness and background tasks.
//!
//! `random()` gives numbers that look random but are the same on every run:
//! they come from the project's seed, so renders are reproducible. `spawn`
//! starts an animation in the background while the scene continues.

use oxframe::prelude::*;

#[oxframe::scene]
pub async fn random_lesson(view: View) {
    // Confetti in the background.
    let rain = spawn(async move {
        for _ in 0..60 {
            let x = random_range(-800.0, 800.0);
            let c = Color::rgb(random() as f32, 0.6, 1.0 - random() as f32);
            let piece = view.add(
                Rect::new()
                    .size(random_range(12.0, 28.0))
                    .position((x, -600.0))
                    .fill(c),
            );
            // Each piece falls on its own, also in the background.
            spawn(async move {
                all((
                    piece.y().to(600.0, 2.0).ease(presets::LINEAR),
                    piece.rotation().to(360.0, 2.0),
                ))
                .await;
                piece.remove();
            });
            wait_for(0.05).await;
        }
    });

    // Meanwhile, in the foreground:
    let title = view.add(
        Txt::new()
            .text("seed = 7")
            .font_size(110.0)
            .weight(800.0)
            .scale(0.0),
    );
    title
        .scale()
        .to(Vec2::new(1.0, 1.0), 0.8)
        .ease(presets::OUT_BACK)
        .await;
    rain.await;
    wait_for(2.0).await;
}

// Try: change `.seed(7)` below to another number — a different, but again
// repeatable, confetti.

pub fn project() -> Project {
    Project::new("random").seed(7).scene(random_lesson())
}

Random, but repeatable

random() gives a number between 0 and 1, random_range(a, b) one between a and b. They look random, but they come from the project's seed (Project::new(..).seed(7)), so every run — in the editor, in the final render, on another computer — produces exactly the same confetti. Change the seed for a different, again repeatable, result.

Inside Custom, use ctx.rng(seed) instead. random() only runs while the scene is stepping, and paint happens after that. A spray that keeps its own motion is a Particles node, not a counter in the closure.

This is a rule worth knowing: oxframe renders the same frames every time. That is why scenes must not use the clock or other sources of real randomness.

Background tasks

spawn(async move { ... }) starts an animation that runs on its own, while the scene continues with the next line. Each confetti piece is spawned, falls, and removes itself. spawn returns a handle you can .await later to wait for the task — here the title waits for the confetti to finish.

Next

One scene is not a whole video yet. Lesson 11 puts several scenes in a row and blends them together.

Edit this page on GitHub