🥚 Moonshine Spawn
Collection of tools for spawning entities in Bevy.
⚠️ Deprecated
This crate is deprecated in favor of the upcoming BSN changes to Bevy.
In the meantime, prefer to use i-can't-believe-its-not-bsn as it is a closer implementation to BSN and will make future migration easier.
The only feature of this library that is not covered by BSN are spawn keys. Implementation of spawn keys on user code should be trivial, especially with 🏷️ Moonshine Tag.
Overview
In Bevy, complex hierarchies of entities are typically spawned using the ChildBuilder:
use *;
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. Arguably, this makes the code less modular and harder to read and maintain.
Additionally, there is no built-in functionality to reference a predefined entity hierarchy (i.e. a "prefab").
This crate aims to solve some of these issues by providing tools to make spawning more ergonomic:
use *;
use *;
let mut app = new;
// Make sure `SpawnPlugin` is added to your `App`:
app.add_plugins;
// Register spawnables during initialization:
let chicken_key: SpawnKey = app.add_spawnable;
// Spawn a spawnable with a key:
let chicken = app.world_mut.spawn_key; // .spawn_key("chicken") also works!
;
Usage
Spawnables
A type is a "spawnable" if it implements either Spawn or SpawnOnce:
SpawnOnce is implemented by default for all Bevy bundles.
Spawn is implemented for all types which implement SpawnOnce + Clone. This means any Bundle + Clone implements Spawn.
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:
use *;
use *;
;
;
;
Bundles + Children
To spawn bundles with children, use the WithChildren trait:
use *;
use *;
;
Or use the SpawnChildren component and the spawn_children function:
use *;
use *;
;
Spawn Keys
A SpawnKey is a reference to a registered spawnable.
Each key must be unique within the scope of a World and is registered using the AddSpawnable extension trait.
Use this to register your spawnables during app initialization:
app.add_spawnable;
You can then spawn a spawnable using a spawn key at runtime, either using Commands or a &mut World:
commands.spawn_with_key;
You may also use spawn keys when spawning children of a bundle:
force_spawn_children
This crate works by running a system which invokes any SpawnChildren Component during the First schedule.
Sometimes it may be necessary to spawn children manually before the First schedule runs due to system dependencies.
In such cases, you may use force_spawn_children to manually invoke these components:
use *;
use ;
let mut app = new;
// This system spawns a chicken during setup:
// This system depends on children of `Chicken`:
;
;
let mut app = new;
app.add_plugins
.add_systems
// Without `force_spawn_children`, chicken head would only be updated on the second update cycle after spawn.
// With `force_spawn_children`, chicken head would be updated in the same update cycle.
.add_systems
.add_systems;
Support
Please post an issue for any bugs, questions, or suggestions.
You may also contact me on the official Bevy Discord server as @Zeenobit.