Expand description
Serializable entity configuration for the Bevy game engine.
This crate provides several abstractions for specifying serializable entities and components:
- The
ProtoComponenttrait provides methods to load components from assets. - The
ProtoDeserializertrait describes component deserialization. ProtoPluginprovides configuration for asset loading.
§Examples
Define a serialized prototype:
# assets/prototypes/simple-enemy.yaml
name: "Simple Enemy"
components:
- type: Enemy
- type: Attack
value:
damage: 10Implement 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§
- data
- Provides resource and deserialization for prototype data.
- prelude
- Includes all public types and the macro to derive
ProtoComponent.
Structs§
- Proto
Plugin - Inserts resources for loading prototypes.
- Prototype
- The default prototype object, providing the basics for the prototype system.
Traits§
- Proto
Component - Specifies how a type inserts components into an entity.
- Prototypical
- Allows access to a prototype’s name and components so that it can be spawned in
Functions§
- deserialize_
templates_ list - A function used to deserialize a list of templates.
Derive Macros§
- Proto
Component - Automatically implements
ProtoComponentfor the given struct or enum. This works on all structs and enums, including tuple and unit structs.