Skip to content

Instantly share code, notes, and snippets.

@tomara-x
Created June 17, 2024 16:23
Show Gist options
  • Save tomara-x/3d3846d58a44b55fd9f0b0dbb17b767b to your computer and use it in GitHub Desktop.
Save tomara-x/3d3846d58a44b55fd9f0b0dbb17b767b to your computer and use it in GitHub Desktop.
despawn grownups
[package]
name = "smol"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = "0.14.0-rc.3"
//use std::{fs::File, io::Write};
use bevy::{
//tasks::IoTaskPool,
scene::SceneInstance,
prelude::*,
};
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct N(String);
fn main() {
App::new()
.add_plugins(DefaultPlugins)
//.add_systems(Startup, save_scene)
.add_systems(Startup, load_scene)
.add_systems(Update, print)
.add_systems(Update, remove_parent)
.add_systems(Update, despawn)
.register_type::<N>()
.run();
}
fn load_scene(
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
commands.spawn(DynamicSceneBundle {
scene: asset_server.load("test"),
..default()
});
}
//fn save_scene(world: &mut World) {
// world.spawn(N(String::from("cat")));
// world.spawn(N(String::from("dog")));
// world.spawn(N(String::from("isopod")));
// let mut query = world.query_filtered::<Entity, With<N>>();
// let scene = DynamicSceneBuilder::from_world(world)
// .allow::<N>()
// .extract_entities(query.iter(world))
// .build();
// let type_registry = world.resource::<AppTypeRegistry>();
// let type_registry = type_registry.read();
// let serialized_scene = scene.serialize(&type_registry).unwrap();
//
// IoTaskPool::get()
// .spawn(async move {
// File::create("assets/test")
// .and_then(|mut file| file.write(serialized_scene.as_bytes()))
// .expect("Error while writing scene to file");
// })
// .detach();
//}
fn print(
entity_query: Query<Entity>,
children_query: Query<(Entity, &Children)>,
n_query: Query<(Entity, &N)>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
if keyboard_input.just_pressed(KeyCode::KeyP) {
info!("entity count: {}", entity_query.iter().len());
for (e, c) in children_query.iter() {
info!("entity: {} children: {:?}", e, c);
}
for (e, n) in n_query.iter() {
info!("entity: {} name: {}", e, n.0);
}
}
}
fn remove_parent(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
children_query: Query<&Children>,
) {
if keyboard_input.just_pressed(KeyCode::KeyR) {
if let Ok(children) = children_query.get_single() {
for child in children {
commands.entity(*child).remove_parent();
}
}
}
}
fn despawn(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut commands: Commands,
instance_query: Query<Entity, With<SceneInstance>>,
) {
if keyboard_input.just_pressed(KeyCode::KeyD) {
if let Ok(instance) = instance_query.get_single() {
commands.entity(instance).despawn();
}
}
}
after running and pressing p:
2024-06-17T16:11:46.407637Z INFO smol: entity count: 5
2024-06-17T16:11:46.407714Z INFO smol: entity: 1v1|4294967297 children: Children([Entity { index: 2, generation: 1 }, Entity { index: 3, generation: 1 }, Entity { index: 4, generation: 1 }])
2024-06-17T16:11:46.407759Z INFO smol: entity: 2v1|4294967298 name: cat
2024-06-17T16:11:46.407778Z INFO smol: entity: 3v1|4294967299 name: dog
2024-06-17T16:11:46.407800Z INFO smol: entity: 4v1|4294967300 name: isopod
after pressing r, then p:
2024-06-17T16:13:12.828458Z INFO smol: entity count: 5
2024-06-17T16:13:12.828518Z INFO smol: entity: 2v1|4294967298 name: cat
2024-06-17T16:13:12.828542Z INFO smol: entity: 3v1|4294967299 name: dog
2024-06-17T16:13:12.828563Z INFO smol: entity: 4v1|4294967300 name: isopod
after pressing d, then p:
2024-06-17T16:13:40.513944Z INFO smol: entity count: 1
(
resources: {},
entities: {
4294967297: (
components: {
"smol::N": ("cat"),
},
),
4294967298: (
components: {
"smol::N": ("dog"),
},
),
4294967299: (
components: {
"smol::N": ("isopod"),
},
),
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment