Crate bevy_proto_typetag

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:

§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§

data
Provides resource and deserialization for prototype data.
prelude
Includes all public types and the macro to derive ProtoComponent.

Structs§

ProtoPlugin
Inserts resources for loading prototypes.
Prototype
The default prototype object, providing the basics for the prototype system.

Traits§

ProtoComponent
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§

ProtoComponent
Automatically implements ProtoComponent for the given struct or enum. This works on all structs and enums, including tuple and unit structs.