Crate bevy_proto_typetag
source ·Expand description
Serializable entity configuration for the Bevy game engine.
This crate provides several abstractions for specifying serializable entities and components:
- The
ProtoComponent
trait provides methods to load components from assets. - The
ProtoDeserializer
trait describes component deserialization. ProtoPlugin
provides configuration for asset loading.
Examples
Define a serialized prototype:
# assets/prototypes/simple-enemy.yaml
name: "Simple Enemy"
components:
- type: Enemy
- type: Attack
value:
damage: 10
Implement ProtoComponent
for the component types:
use bevy::prelude::*;
use bevy_proto_typetag::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize, ProtoComponent, Component)]
struct Enemy;
#[derive(Clone, Serialize, Deserialize, ProtoComponent, Component)]
struct Attack {
damage: u16
}
Add the plugin:
use bevy::prelude::*;
use bevy_proto_typetag::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(ProtoPlugin {
options: Some(ProtoDataOptions {
// You can also change the prototype directories here
directories: vec![String::from("assets/prototypes")],
// And specify whether you want the prototype files to be recursively loaded
recursive_loading: false,
// You can also update the allowed extensions within those directories
extensions: Some(vec!["yaml", "json"]),
..ProtoDataOptions::default()
})
});
}
Finally, spawn a prototype with a system:
use bevy::prelude::*;
use bevy_proto_typetag::prelude::*;
fn spawn_enemy(mut commands: Commands, data: Res<ProtoData>, asset_server: Res<AssetServer>) {
let proto = data.get_prototype("Simple Enemy").expect("Prototype doesn't exist!");
// Spawns in our "Simple Enemy" Prototype
proto.spawn(&mut commands, &data, &asset_server);
}
Modules
- Provides resource and deserialization for prototype data.
- Includes all public types and the macro to derive
ProtoComponent
.
Structs
- Inserts resources for loading prototypes.
- The default prototype object, providing the basics for the prototype system.
Traits
- Specifies how a type inserts components into an entity.
- Allows access to a prototype’s name and components so that it can be spawned in
Functions
- A function used to deserialize a list of templates.
Derive Macros
- Automatically implements
ProtoComponent
for the given struct or enum. This works on all structs and enums, including tuple and unit structs.