Bevy_save
A framework for saving and loading application state in Bevy.
Features
[NEW]: Flows
When creating a complex application, snapshot builder and applier functions tend to get complex and unwieldy.
Flows are chains of systems used to modularize this process, allowing you to build snapshots and apply them in stages.
They are defined similar to Bevy systems, but they require an input and an output.
Additionally, the introduction of Flows allows reflection to become optional - bring your own serialization if you so wish!
// User-defined captures make reflection unnecessary
// Flow systems have full access to the ECS (even write access)
// Flow systems can be added to flows from anywhere, not just in one location
;
[NEW]: Pathway
Pathway is the more flexible version of Pipeline which allows you to specify your own capture type and use Flows.
// Pathways look very similar to pipelines, but there are a few key differences
;
// Flow labels don't encode any behavior by themselves, only point to flows
;
;
Save file management
bevy_save automatically uses your app's workspace name to create a unique, permanent save location in the correct place for any platform it can run on.
By default, World::save() and World::load() uses the managed save file location to save and load your application state, handling all serialization and deserialization for you.
Save directory location
With the default FileIO backend, your save directory is managed for you.
WORKSPACE is the name of your project's workspace (parent folder) name.
| Windows | Linux/*BSD | MacOS |
|---|---|---|
C:\Users\%USERNAME%\AppData\Local\WORKSPACE\saves |
~/.local/share/WORKSPACE/saves |
~/Library/Application Support/WORKSPACE/saves |
On WASM, snapshots are saved to LocalStorage, with the key WORKSPACE.KEY.
Reflection-based Snapshots
bevy_save is not just about save files, it is about total control over application state.
With the "reflect" feature enabled, this crate introduces a snapshot type which may be used directly:
Snapshotis a serializable snapshot of all saveable resources, entities, and components.
Or via the World extension method WorldPathwayExt and WorldCheckpointExt:
World::capture()captures a snapshot of the current application state, including resources.World::apply()applies a snapshot to theWorld.
Rollbacks and checkpoints
With the "checkpoints" feature enabled, this crate provides methods for creating checkpoints which are ordered and can be rolled back / forwards through.
World::checkpoint()captures a snapshot for later rollback / rollforward.World::rollback()rolls the application state backwards or forwards through any checkpoints you have created.
The Checkpoints resource also gives you fine-tuned control of the currently stored rollback checkpoints.
Type registration
No special traits or NewTypes necessary, bevy_save takes full advantage of Bevy's built-in reflection.
As long as the type implements Reflect, it can be registered and used with bevy_save.
bevy_save provides extension traits for App allowing you to do so.
App.init_pipeline::<P>()initializes aPipelinefor use with save / load.App.allow_checkpoint::<T>()allows a type to roll back.App.deny_checkpoint::<T>()denies a type from rolling back.
Pipeline
The Pipeline trait allows you to use multiple different configurations of Backend and Format in the same App.
Using Pipeline also lets you re-use Snapshot appliers and builders.
;
Type Filtering and Partial Snapshots
While bevy_save aims to make it as easy as possible to save your entire world, some applications also need to be able to save only parts of the world.
Builder allows you to manually create snapshots like DynamicSceneBuilder:
Entity hooks
You are also able to add hooks when applying snapshots, similar to bevy-scene-hook.
This can be used for many things, like spawning the snapshot as a child of an entity:
let snapshot = from_world;
snapshot
.applier
// This will be run for every Entity in the snapshot
// It runs after the Entity's Components are loaded
.hook
.apply;
Hooks may also despawn entities:
let snapshot = from_world;
snapshot
.applier
.hook
Entity mapping
As Entity ids are not intended to be used as unique identifiers, bevy_save supports mapping Entity ids.
First, you'll need to get a ApplierRef:
The ApplierRef will then allow you to configure the entity map (and other settings) before applying:
let snapshot = from_world;
snapshot
.applier
// Your entity map
.entity_map
// Despawn all entities matching (With<A>, Without<B>)
.
.apply;
MapEntities
bevy_save also supports MapEntities via reflection to allow you to update entity ids within components and resources.
See Bevy's Parent Component for a simple example.
Backend
The Backend is the interface between your application and persistent storage.
Some example backends may include FileIO, sqlite, LocalStorage, or network storage.
;
Format
Format is how your application serializes and deserializes your data.
Formats can either be human-readable like JSON or binary like MessagePack.
;
Prefabs
The Prefab trait allows you to easily spawn entities from a blueprint.
;
You are also able to extract resources by type:
builder
// Extract the resource by the type name
// In this case, we extract the resource from the `manual` example
.
// Build the `Snapshot`
// It will only contain the one resource we extracted
.build
Additionally, explicit type filtering like ApplierRef is available when building snapshots:
builder
// Exclude `Transform` from this `Snapshot`
.
// Extract all matching entities and resources
.extract_all
// Clear all extracted entities without any components
.clear_empty
// Build the `Snapshot`
.build
Stability warning
bevy_save does not yet provide any stability guarantees for save file format between crate versions.
bevy_save relies on serialization to create save files and as such is exposed to internal implementation details for types.
Expect Bevy or other crate updates to break your save file format.
It should be possible to mitigate this by overriding ReflectDeserialize for any offending types.
Changing what entities have what components or how you use your entities or resources in your logic can also result in broken saves.
While bevy_save does not yet have explicit support for save file migration, you can use ApplierRef::hook to account for changes while applying a snapshot.
If your application has specific migration requirements, please open an issue.
Entity
For all intents and purposes,
Entityshould be treated as an opaque identifier. The internal bit representation is liable to change from release to release as are the behaviors or performance characteristics of any of its trait implementations (i.e.Ord,Hash,etc.). This means that changes inEntity’s representation, though made readable through various functions on the type, are not considered breaking changes under SemVer.In particular, directly serializing with
SerializeandDeserializemake zero guarantee of long term wire format compatibility. Changes in behavior will cause serializedEntityvalues persisted to long term storage (i.e. disk, databases, etc.) will fail to deserialize upon being updated.
bevy_save serializes and deserializes entities directly. If you need to maintain compatibility across Bevy versions, consider adding a unique identifier Component to your tracked entities.
Stabilization
bevy_save will become a candidate for stabilization once all stabilization tasks have been completed.
Compatibility
Bevy
| Bevy Version | Crate Version |
|---|---|
0.16 |
0.18 |
0.15 |
0.16 2, 0.17 |
0.14 1 |
0.15 |
0.13 |
0.14 |
0.12 |
0.10, 0.11, 0.12, 0.13 |
0.11 |
0.9 |
0.10 |
0.4, 0.5, 0.6, 0.7, 0.8 |
0.9 |
0.1, 0.2, 0.3 |
Save format changes (since 0.15)
-
bevychangedEntity's on-disk representation -
bevy_savebegan usingFromReflectwhen taking snapshots
Migration
0.18 - 0.19
This version introduced Pathway, which is effectively a superset of Pipeline.
- In
World::capture,World:apply,World::save,World::loadmethods and similar, add a&before your existing pipeline - Previously provided
Commandsextension traits and associated commands have been removed (sincePathwayoperates on references), you'll need to write your own or use events instead - If you're using
default-features = false, you'll need to add thereflectandcheckpointsfeatures in order to get parity with the last version
Platforms
| Platform | Support |
|---|---|
| Windows | Yes |
| MacOS | Yes |
| Linux | Yes |
| WASM | Yes |
| Android | No |
| iOS | No |
Feature Flags
| Feature flag | Description | Default? |
|---|---|---|
reflect |
Enables reflection-based snapshots | Yes |
checkpoints |
Enables reflection-based checkpoints | Yes |
bevy_asset |
Enables bevy_asset type registration |
Yes |
bevy_render |
Enables bevy_render type registration |
Yes |
bevy_sprite |
Enables bevy_sprite type registration |
Yes |
brotli |
Enables Brotli compression middleware |
No |
License
bevy_save is dual-licensed under MIT and Apache-2.0.