# Bevy API reference

The whole authoring surface is one plugin, one asset, five components (two of them optional), one resource, two state components, four feedback messages and four command messages. Generated docs: [**rustdoc for `ssplayer_bevy`**](<https://cri-middleware.github.io/SSPlayerForWgpu/api/ssplayer_bevy/index.html>).

**Bevy 0.20.x, and no other line** — see [Get started with Bevy](<https://cri-middleware.github.io/SSPlayerForWgpu/start/bevy/index.md#supported-bevy>).

## Plugin

```rust
app.add_plugins(SsPlayerPlugin);
```

It adds the extract, prepare and draw systems, and owns the device-scoped renderer and the target-scoped scene inside the render world. Add it after `DefaultPlugins`; without the render app it logs an error and does nothing. Nothing else is required.

The camera, however, needs two things: `Msaa::Off` (the player's pipelines are single-sampled) and `CompositingSpace::Srgb` (or every character is visibly too bright).

## Components

### `SsPlayer` — what to play

| Field |  |  |
| --- | --- | --- |
| `pack: Handle<SsPack>` | the `.ssab`, from `asset_server.load(...)` | **Changing it rebuilds the playback tree**, once the new pack has loaded |
| `animation: String` | animation to select. Empty means "not yet" | The same, unless `transition_ms` is set. Only a change of value acts, so a system may write it every frame |
| `transition_ms: f32` | how a change of `animation` is made: `0` (the default) switches at once; above zero crossfades over that many milliseconds | The first animation, and the first after a change of `pack`, is always switched to at once |

`animation` is the only way to change track 0's animation: a `TrackCommand` that would lay one on track 0 is refused, so what it names and what track 0 plays never part.

`SsTransform`, `SsPlayback`, `SsPlaybackState` and `SsTrackState` are **required components**, so spawning `SsPlayer` alone gives you the other four at their defaults.

### `SsPack` — the `.ssab` as an asset

A `.ssab` loads through Bevy's `AssetServer`, from whatever source the app configures. The loader runs the player's own walk: it reads the packs the pack's instance parts play from, and requests each atlas as a Bevy `Image` — non-sRGB, because the player takes atlas bytes as authored.

- A character is built only once its pack and every atlas have settled, so it never shows a frame drawn without them. An atlas that failed to load draws white.
- Characters sharing a handle share one parse of each pack and one GPU texture per atlas, lent to the player rather than copied.
- With Bevy's `file_watcher` feature, changing the `.ssab` or a sibling pack rebuilds the characters playing it; changing an atlas re-lends it in place.
- `SsPack::pack()` is the parsed pack, for its name and `animation_names()`.
- `SsPackLoader` is the loader the plugin registers for `.ssab`. Only the `.ssab` itself can fail to load — `SsPackLoadError::Io` when it cannot be read, `::Parse` when it is not a valid pack — and Bevy reports that as an `AssetLoadFailedEvent<SsPack>`. A sibling pack that does not arrive is not an error: the instance parts playing it do not appear.

### `SsTransform` — where it sits

| Field | Default |
| --- | --- |
| `x`, `y` | `0.0` |
| `scale_x`, `scale_y` | `1.0` |
| `rotation` (radians) | `0.0` |

`x` and `y` are measured from the view's centre in Bevy's world units — logical pixels under the default 2D projection — and `y` runs **downward**, as `SSPlayer::set_transform`'s does, opposite to Bevy's world. Free to change, every frame if you like.

### `SsPlayback` — transport and compositing

| Field | Default |  |
| --- | --- | --- |
| `playing: bool` | `true` | Pauses and resumes **in place**. Never rewinds, so it cannot replay a finished animation |
| `speed_scale: f32` | `1.0` | A multiple of the authored rate. `0` and negatives stop |
| `order: i32` | `0` | Draw order; lower draws first. Ties keep extraction order |
| `alpha: f32` | `1.0` | Whole-character fade — part by part, unless [`SsSceneSettings::group_alpha`](<#ssscenesettings>) is on |
| `tint: u32` | `0xFFFFFF` | Whole-character tint, `0xRRGGBB`, multiplied in after PartsColor |
| `visible: bool` | `true` | False skips the draw; the timeline keeps running |
| `culled: bool` | `false` | True skips **the timeline advance** as well as the draw — for an off-screen character |
| `restart: u32` | `0` | **Bump it** (any change of value) to replay from the head, re-arming the loop count |
| `direction: PlaybackDirection` | `Forward` | Which way the playhead walks: `Forward` or `Backward` |
| `style: PlaybackStyle` | `Normal` | `Normal` wraps at the far end, `PingPong` turns back |
| `loop_count: i32` | `-1` | Cycles before stopping; `-1` or `0` loops for ever. A change re-arms the count |
| `frame_rate: i32` | `0` | Replaces the authored rate; `0` keeps it. Prefer `speed_scale` |
| `frame_skip_enabled: bool` | `true` | Off advances one frame per tick instead of keeping to the wall clock |
| `sub_frame_enabled: bool` | `true` | Off floors the frame that is drawn |
| `animation_section: Option<(i32, i32)>` | `None` | Restrict the playhead to `start..=end` of the current animation. Setting it puts the playhead at its start. An end before the start, both in range, is refused with a warning, once, and the section stays as it was |

`restart` is a generation counter rather than a flag so a host can re-trigger a one-shot at will with nothing to clear afterwards, and so two restarts in a row are two restarts.

### `SsPlaybackState` — what playback did (polled)

| Field |  |
| --- | --- |
| `frame_no: f32` | the frame the timeline last landed on |
| `finished: bool` | whether the animation has reached its end |
| `playing_forward: bool` | which way the playhead is actually travelling — false on a ping-pong's return leg too. Gate audio on it |

### `SsTrackState` — the track stack (polled)

What [`SsTrackCommand`](<#motion-blending>) built, refreshed every frame and **one frame behind**.

| Field |  |
| --- | --- |
| `tracks: Vec<SsTrackInfo>` | one per track, track 0 first: `main_source`, `alpha`, `blend`, `sync`, `frame`, `fade_progress` (`None` when not fading), the playback settings `speed`, `frame_rate`, `frame_skip_enabled`, `direction` and `style`, `loop_count` (`None` on an empty track), `finished`, `just_looped` and `outgoing_event_threshold` |
| `sources: Vec<SsSourceInfo>` | the sources laid with `TrackCommand::AddSource` that are still playing: `source` (the handle), `track`, `animation`, `weight`, `frame` |

### `SsCellmapTextures` — runtime texture replacement (optional)

Swap the whole texture a cell map binds — a costume or palette change — for a Bevy image.

| Field |  |
| --- | --- |
| `overrides: HashMap<String, Handle<Image>>` | cell map (or TextureChange-target) name → the image to bind for it |

Insert an entry to override, remove it (or the whole component) to restore. An image still loading is applied the frame it becomes ready, so a freshly-requested handle is fine. The swap is per-character, and assumes a straight-alpha Bevy image **loaded with `is_srgb: false`** — an sRGB one draws too dark, and the adapter warns once. Only characters that swap textures need spawn it. See [Texture replacement](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/texture-replacement/index.md>).

### `SsCrowd` — one character at many placements (optional)

| Field |  |
| --- | --- |
| `members: Vec<CrowdMember>` | where each copy stands, in `SsTransform`'s terms and composed under the character's own, and its `alpha` and `tint` on top of `SsPlayback`'s. Empty draws the character once |

One playhead, one evaluation and one character's draw calls, however many members — everything but placement, alpha and tint is shared, events included. `CrowdMember` is re-exported from the player crate. Moving the members every frame allocates nothing unless the crowd grows. Only characters drawn as crowds need spawn it. See [Crowds](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/crowds/index.md>).

## Resource

### `SsSceneSettings` — how the scene draws

| Field | Default |  |
| --- | --- | --- |
| `group_alpha: bool` | `false` | Fade each character whose `alpha` is below 1 as one layer, composited once, rather than part by part |

Present from the moment the plugin is added; change it whenever. Each character faded at once costs a render pass per frame and a layer the size of the view. See [Whole-character tint and alpha](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/part-overrides/index.md#whole-character-tint-and-alpha>).

## Messages (reactive)

| Message | Carries |
| --- | --- |
| `SsEventMessage` | `entity`, `event: SsEvent` — user data, signal or audio |
| `SsStarted` | `entity` — the start **edge**: a new animation or pack while `playing`, `playing` turned on for the first time, an `SsPlay`, or a bumped `restart`. Not a resume, and not a new animation with `transition_ms` above 0, which crossfades on the tree that is already playing |
| `SsFinished` | `entity` — the completion **edge** |
| `SsLooped` | `entity` — the loop boundary edge |

Three commands go the other way, from gameplay to the character. They are messages rather than fields, because each happens once rather than being a state the character is in. One sent before the pack has loaded is applied once the animation is set up, and of several of a kind in one frame, the last wins:

| Message | Carries |
| --- | --- |
| `SsPlay` | `entity`, `start_frame: Option<f32>` — **play**: rewind to the head of the section, or to `start_frame`, re-arm the loop count and start, even a finished or stopped animation. `None` is what bumping `restart` does. `playing` still governs afterwards |
| `SsStop` | `entity` — **stop**: end playback where it stands. The frame stays drawn, and turning `playing` off and on does not start it again; `SsPlay` or `restart` does |
| `SsSeek` | `entity`, `frame: f32` — move the playhead there. Fractional frames are honoured, and it stays inside the section |

Sent together in one frame they apply as play, stop, seek.

### Motion blending

`SsTrackCommand { entity, command }` performs one operation on the character's track stack; `command` is a `TrackCommand`, each variant the `SSPlayer` call of the same name — `SetAnimation`, `Crossfade`, `FadeTrack`, `CancelFade`, `Clear`, `SetAlpha`, `SetBlend` (`TrackBlend::Mix` or `Additive`), `SetSync` (`TrackSync::Free` or `Start`), `SetFrame`, `SetSpeed`, `SetFrameRate`, `SetFrameSkipEnabled`, `SetPlaybackDirection`, `SetLoopCount`, `SetOutgoingEventThreshold`, `AddSource`, `SetSourceWeight`, `SetSourceFrame`, `SetSourceEventThreshold`. `SetAnimation` and `Crossfade` are refused on track 0, which `SsPlayer::animation` sets, and so are `SetSpeed`, `SetFrameRate`, `SetFrameSkipEnabled`, `SetPlaybackDirection` and `SetLoopCount`: track 0's playback settings and loop count are `SsPlayback`'s. Unlike the three above, **every one applies, in the order written**, and ones sent before the pack has loaded wait for the animation. A refused one is logged. The result comes back in [`SsTrackState`](<#sstrackstate>), where an `AddSource` finds its handle.

```rust
fn run(mut tracks: MessageWriter<SsTrackCommand>, character: Single<(Entity, &mut SsPlayer)>) {
    let (entity, mut player) = character.into_inner();
    tracks.write(SsTrackCommand { entity, command: TrackCommand::SetSync { track: 0, sync: TrackSync::Start } });
    // Track 0 crossfades through the component.
    player.transition_ms = 300.0;
    player.animation = "run".into();
}

fn react(mut tracks: MessageWriter<SsTrackCommand>, character: Single<Entity, With<SsPlayer>>) {
    let entity = *character;
    for command in [
        TrackCommand::SetAnimation { track: 1, animation: "hit_react".into() },
        TrackCommand::SetLoopCount { track: 1, count: 1 },   // once, then hold the last frame
    ] {
        tracks.write(SsTrackCommand { entity, command });
    }
}
```

**Changing `SsPlayer::animation` with `transition_ms` at 0 resets the stack** to track 0 alone, as `set_animation` does; send the overlays and track settings again after it. With `transition_ms` set the stack stays, as `crossfade` leaves it. See [Motion blending](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/motion-blending/index.md>).

```rust
fn jump(mut seeks: MessageWriter<SsSeek>, character: Single<Entity, With<SsPlayer>>) {
    seeks.write(SsSeek { entity: *character, frame: 18.0 });
}
```

```rust
fn on_finish(mut finished: MessageReader<SsFinished>, mut commands: Commands) {
    for m in finished.read() {
        commands.entity(m.entity).despawn();
    }
}
```

> [!WARNING]
>
> **Both feedback surfaces are one frame behind**
>
> Playback runs in the render world, after that frame's `Update`. What you read this frame is what playback produced last frame. For a sound cue or a state change that is imperceptible; a host that cannot tolerate it would need playback in the main world, which this adapter does not do.

## Where the work happens

| Schedule | What |
| --- | --- |
| Main world | the components and the resource above — the whole authoring surface |
| `ExtractSchedule` | copies them, plus the frame delta, into the render world |
| `Render` / `RenderSystems::Prepare` | applies them to the playback trees; rebuilds a character whose `pack` or `animation` changed |
| `Core2d` / `Core2dSystems::MainPass` | advances, builds and draws — ordered **after** `main_transparent_pass_2d` |

That last ordering is an explicit edge, not a coincidence of set membership: two systems that each take a `RenderContext` get their own command encoder, and only an explicit edge decides which buffer reaches the queue first. Without it, whether characters land over or under Bevy's sprites would be a race.

A window resize needs nothing from you: the draw system reprojects to the viewport's size every frame.

## What this adapter does not do yet

|  |  |
| --- | --- |
| **One view** | The adapter holds one `SSScene` with every character in it, and `SSScene::update` fuses the timeline advance with the geometry build, so a second view would advance every animation twice per frame. Later views are skipped, with a warning — so a UI camera, a render-to-texture portrait, a minimap or a split screen leaves every character in whichever view came first |
| **Screen space** | Characters are placed from the view's centre, whatever the camera does: its `Transform` and its projection's `scale` do not move or zoom them, and `Visibility` and `RenderLayers` do not hide them. Hide one with `SsPlayback::visible`, and move it with `SsTransform` |
| **Depth among sprites** | Characters draw over all of Bevy's 2D — sprites, `Mesh2d`, `Text2d` — in one pass after `main_transparent_pass_2d`, so a sprite cannot sit in front of one |
| **MSAA** | must be off. The adapter warns once and skips a multisampled target rather than crashing |
| **Part overrides** | not exposed as components. See [Scopes and names](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/concepts/index.md#what-each-face-can-do>) |

## Masks and device loss

Masks use the **coverage** backend automatically, because Bevy's 2D depth texture carries no stencil; the cost is one extra render pass per masking character per frame ([Masks](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/masks/index.md>)).

Device loss recovers by riding Bevy's own recovery, but **you must opt in** with a `RenderErrorHandler` that answers a `DeviceLost` error with `RenderErrorPolicy::Recover(RenderCreation::default())` ([Device loss](<https://cri-middleware.github.io/SSPlayerForWgpu/guide/device-loss/index.md>)).
