Bevy Scene hook
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
- Add the crate to your dependencies
[]
= "7"
- Add the plugin
.add_plugin
Example
use ;
;
;
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:
use ;
;
;
Note that bevy-scene-hook also has a few items defined for user convinience:
HookedSceneBundleHookedSceneStateis_scene_hooked
Those extra items are all defined in lib.rs.
Change log
1.1.0: Addis_loadedmethod toSceneInstance1.2.0: Add theworldmodule containing aSceneHooktrait that has exclusive world access. Useful if you want access to assets for example.2.0.0: Breaking: bump bevy version to0.7(you should be able to upgrade from1.2.0without changing your code)3.0.0: Breaking: completely rework the crate.- Remove the
worldmodule, as the base hook method has become much more powerful. - Rename
SceneHooktoHook, nowHookhas a unique method to implement. - You don't have to add any system yourself, now you have to add the
HookPluginplugin to your app. - Move the API exclusively to a new
SystemParam:HookingSceneSpawner. Please use that parameter to add and remove scenes that contain hooks. (please tell if you you accidentally spell itHonkingSceneSpawnermore than once :duck:) - Moved the
when_spawnedrun criteria to theis_scene_hookedfunction exposed at the root of the crate, theHookedSceneStatesystem parameter or theSceneLoadedcomponent. Please use any of those three instead ofwhen_spawned. - Now supports passing closures as hooks, instead of having to define a trait each time.
- Now supports adding multiple of the same scene! Doesn't handle hot-reloading, but that's alright since bevy's scene hot-reloading is currently broken anyway :D
- Remove the
3.1.0: makerun_hookssystem public so that it's possible to add it to any stage you want in relation to any other system you want.4.0.0: Breaking: bump bevy version to0.8- Uses the new scene bundle system
- Rename
SceneLoadedtoSceneHooked. - Removed the
Hooktrait, nowSceneHook::newaccepts a closure. - Instead of using
HookingSceneSpawner, usesHookedSceneBundleand spawn it into an entity.
4.1.0: AddHookedDynamicSceneBundleto use withDynamicScenes. Thanks Shatur (#3)5.0.0: Breaking: bump bevy version to0.95.1.1: My bad, I accidentally published to version5.1.0instead of5.0.05.1.2: Fix scenes never triggering hooks due to a missing check. Thanks sdfgeoff (#5) If you depend onbevy-scene-hookas a cargo dependency, you must runcargo updateto get this fix.5.2.0: Add thereloadmodule, definingreload::Hook, a variant ofSceneHookthat handles gracefully reloads and unloads.6.0.0: Breaking: bump bevy version to0.10.7.0.0: Breaking: bump bevy version to0.11.
Version matrix
| bevy | latest supporting version |
|---|---|
| 0.11 | 7.0.0 |
| 0.10 | 6.0.0 |
| 0.9 | 5.2.0 |
| 0.8 | 4.1.0 |
| 0.7 | 3.1.0 |
| 0.6 | 1.2.0 |
License
This library is licensed under Apache 2.0.