Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity Inspector #24

Open
LiamGallagher737 opened this issue Apr 19, 2024 · 0 comments
Open

Entity Inspector #24

LiamGallagher737 opened this issue Apr 19, 2024 · 0 comments
Labels
A-Cross-Cutting Impacts the entire project C-Feature A new feature, making something new possible

Comments

@LiamGallagher737
Copy link
Owner

A way to view the entity hierarchy and components would be very useful for a learning / experimenting tool like this.

How

I previously started working working on this but decided to focus on improving the current state of the playground rather than adding heaps of features. The work can be found on the entity-inspector branch.

This approach was to store a pointer to the Bevy world and have a get_entities method which could be called from js. The following is the extra Rust that would be added before compiling.

static __WORLD_PTR: std::sync::OnceLock::<usize> = std::sync::OnceLock::new();
#[wasm_bindgen::prelude::wasm_bindgen]
pub unsafe fn __get_entities() -> wasm_bindgen::JsValue {
let ptr_usize = __WORLD_PTR.get().unwrap();
let ptr = *ptr_usize as *const bevy::ecs::world::World;
let world = ptr.as_ref().unwrap();
let map = js_sys::Map::new();
for entity in world.iter_entities() {
let id = entity.id();
let components_info = world.inspect_entity(id);
let len = components_info.len() as u32;
let component_names = js_sys::Array::new_with_length(len);
for n in 0..len {
let js_name = wasm_bindgen::JsValue::from_str(components_info[n as usize].name());
component_names.set(n, js_name);
}
map.set(&wasm_bindgen::JsValue::from_str(&format!("{id:?}")), &wasm_bindgen::JsValue::from(component_names));
}
wasm_bindgen::JsValue::from(map)
}
fn __set_world_ptr(world: &bevy::ecs::world::World) {
let _ = __WORLD_PTR.set(world as *const bevy::ecs::world::World as usize);
}
#[derive(Debug)]
#[wasm_bindgen::prelude::wasm_bindgen(getter_with_clone)]
pub struct __EntityInfo {
#[wasm_bindgen(readonly)]
pub index: u32,
#[wasm_bindgen(readonly)]
pub generation: u32,
#[wasm_bindgen(readonly)]
pub component_names: wasm_bindgen::JsValue,
}"#;

For accessing the components we would just use Bevy reflect in a similar way to how I get the entities in the code snippet above.

Visuals (thoughts wanted)

I am unsure of the best way to display the inspector / hierarchy in the playground. It should defiantly be toggleable to avoid taking space when not needed.

@LiamGallagher737 LiamGallagher737 added C-User-Enhancement S-Thoughts-Wanted Extra thinking capacity is needed A-Website Impacts the website A-Compiler Impacts the compiler labels Apr 19, 2024
@LiamGallagher737 LiamGallagher737 linked a pull request Jul 17, 2024 that will close this issue
@LiamGallagher737 LiamGallagher737 added C-Feature A new feature, making something new possible A-Cross-Cutting Impacts the entire project and removed S-Thoughts-Wanted Extra thinking capacity is needed C-User-Enhancement A-Website Impacts the website A-Compiler Impacts the compiler A-Cross-Cutting Impacts the entire project labels Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Cross-Cutting Impacts the entire project C-Feature A new feature, making something new possible
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant