🥚 Moonshine Spawn
A lightweight spawn utility for Bevy.
Overview
In Bevy, complex hierarchies of entities are typically spawned using ChildBuilder pattern:
While this pattern works for most cases, it tends to spread out the logic of entity spawning between the bundle and the function which builds the entity hierarchy which arguably makes the code harder to read and maintain.
There is also no mechanism to statically define an entity hierarchy.
This crate aims to solve both of this issues by introducing statically defined spawnables and allowing bundles to spawn children:
use *;
Features
- Bundles with children
- Statically defined spawnables with serializable spawn keys
- Custom spawners
- Lightweight implementation with minimal boilerplate
Usage
Spawnables
This crate introduces two new traits: Spawn and SpawnOnce:
SpawnOnce is implemented by default for all Bevy bundles. Spawn is implemented for all types which implement SpawnOnce and can be cloned. This means all bundles which allow cloning also implement Spawn.
Any type which implements either of these traits is a "spawnable".
The output of a spawn is always a bundle, which is then inserted into the given entity at the end of spawn process.
You may use these traits to define functional spawnables:
;
Bundles + Children
To spawn bundles with children, use the WithChildren trait:
Or use the SpawnChildren component and the spawn_children function:
Keys
Spawn keys are a way to reference a spawnable by a unique string.
These keys must be unique within the scope of a World and are registered using the RegisterSpawnable extension trait.
Use this to register your spawnables during app initialization:
app.register_spawnable;
You can then spawn a spawnable using a spawn key at runtime, either using Commands, &mut World:
let chicken: EntityCommands = commands.spawn_with_key;
You may also use spawn keys when spawning children of a bundle: