| Crates.io | bevy_easy_database |
| lib.rs | bevy_easy_database |
| version | 0.2.0 |
| created_at | 2025-02-09 17:15:34.169844+00 |
| updated_at | 2025-04-25 21:26:13.296105+00 |
| description | A persistent storage solution for the Bevy game engine that automatically serializes and persists your components to disk using fjall as the underlying database. |
| homepage | |
| repository | https://github.com/MalekiRe/bevy_easy_database |
| max_upload_size | |
| id | 1549116 |
| size | 55,740 |
A persistent storage solution for the Bevy game engine that automatically serializes and persists your components to disk using fjall as the underlying database.
Add this to your Cargo.toml:
[dependencies]
bevy_database = "0.1.1"
bevy = "0.15.2"
use bevy::prelude::*;
use bevy_database::{DatabasePlugin, DatabaseIgnore};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DatabasePlugin) // Add the database plugin
.add_database_mapping::<Transform>() // Register components you want to persist
.run();
}
DatabasePlugin to your Bevy appadd_database_mappingfn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DatabasePlugin)
// Register as many components as you need
.add_database_mapping::<Transform>()
.add_database_mapping::<Player>()
.add_database_mapping::<Score>()
.run();
}
By default, the database is stored in ./database. You can customize this by adding the DatabaseLocation resource:
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DatabasePlugin)
.insert_resource(DatabaseLocation("./my_game_save".to_string()))
.add_database_mapping::<Transform>()
.run();
}
Some entities (like cameras or temporary effects) shouldn't be persisted. Add the DatabaseIgnore component to exclude them:
fn setup(mut commands: Commands) {
// This camera won't be persisted
commands.spawn((Camera2dBundle::default(), DatabaseIgnore));
// This entity will be persisted
commands.spawn(Transform::default());
}
Components are automatically saved when they change:
fn update_position(mut query: Query<&mut Transform>) {
for mut transform in query.iter_mut() {
transform.translation.x += 1.0; // This change will be automatically persisted
}
}
The plugin automatically loads persisted components when your app starts. This means you can:
Serialize and Deserialize from serdeHere's a complete example showing how to create a simple game with persistent entity positions:
use bevy::prelude::*;
use bevy_database::{DatabasePlugin, DatabaseIgnore};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(DatabasePlugin)
.add_systems(Startup, setup)
.add_systems(Update, draw_gizmos)
.add_database_mapping::<Transform>()
.run();
}
fn setup(mut commands: Commands) {
// Camera won't be persisted
commands.spawn((Camera2dBundle::default(), DatabaseIgnore));
// These entities will be persisted
commands.spawn(Transform::from_xyz(0.0, 0.0, 0.0));
commands.spawn(Transform::from_xyz(2.0, 2.0, 0.0));
}
fn draw_gizmos(mut gizmos: Gizmos, transforms: Query<&Transform, Without<Camera>>) {
for transform in transforms.iter() {
gizmos.sphere(transform.translation, 5.0, Color::RED);
}
}
Contributions are welcome! Please feel free to submit a Pull Request.
MIT + Apache License
Built with ❤️ for the Bevy community. Uses fjall for database operations.