Bevy Scene hook

Bevy tracking Latest version Apache 2.0 Documentation

A proof of concept for adding components ad-hoc within code to entities spawned through scenes (such as gltf files) in the [bevy game engine].

If you don't mind adding such a small dependency to your code rather than copy/pasting the code as a module, you can get it from [crates.io].

Usage

toml [dependencies] bevy-scene-hook = "4.1"

Example

```rust use bevyscenehook::{SceneHook, HookedSceneBundle};

enum PileType { Drawing }

[derive(Component)]

struct Pile(PileType);

[derive(Component)]

struct Card;

fn loadscene(mut cmds: Commands, assetserver: Res) { cmds.spawnbundle(HookedSceneBundle { scene: SceneBundle { scene: assetserver.load("scene.glb#Scene0"), ..default() }, hook: SceneHook::new(|entity, cmds| { match entity.get::().map(|t|t.as_str()) { Some("Pile") => cmds.insert(Pile(PileType::Drawing)), Some("Card") => cmds.insert(Card), _ => cmds, }; }), }); } ```

It loads the scene.glb file when the game starts. When the scene is fully loaded, the closure passed to SceneHook::new is ran for each entity present in the scene. We add a Pile component to entities with a Name component of value "Pile".

It is possible to name object in glb scenes in blender using the Outliner dock (the tree view at the top right) and double-clicking object names.

Implementation

bevy-scene-hook is a tinny crate, here is copy/pastable code you can directly vendor in your project:

```rust use bevy::{ prelude::*, scene::SceneInstance, ecs::{world::EntityRef, system::EntityCommands}, };

[derive(Component, Debug)]

pub struct SceneHooked;

[derive(Component)]

pub struct SceneHook { hook: Box, } impl SceneHook { pub fn new(hook: F) -> Self { Self { hook: Box::new(hook) } } }

pub fn runhooks( unloadedinstances: Query<(Entity, &SceneInstance, &SceneHook), Without>, scenemanager: Res, world: &World, mut cmds: Commands, ) { for (entity, instance, hooked) in unloadedinstances.iter() { if let Some(entities) = scenemanager.iterinstanceentities(**instance) { for entityref in entities.filtermap(|e| world.getentity(e)) { let mut cmd = cmds.entity(entityref.id()); (hooked.hook)(&entityref, &mut cmd); } cmds.entity(entity).insert(SceneHooked); } } }

pub struct HookPlugin; impl Plugin for HookPlugin { fn build(&self, app: &mut App) { app.addsystem(runhooks); } } ```

Note that bevy-scene-hook also has a few items defined for user convinience:

Those extra items are all defined in lib.rs.

Change log

Version matrix

| bevy | latest supporting version | |------|--------| | 0.8 | 4.1.0 | | 0.7 | 3.1.0 | | 0.6 | 1.2.0 |

License

This library is licensed under Apache 2.0.