3

是否可以通过使实体处于生锈状态来获取组件列表?例如用于调试目的。

use bevy::prelude::*;
fn main()
{
    App::build()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup.system())
        .add_system(first.system())
        .add_system(first_no_second.system())
        .add_system(first_and_second.system())
        .run()
}

fn setup(mut commands: Commands)
{
    commands.spawn().insert(FirstComponent(0.0));
    commands.spawn().insert(FirstComponent(1.0));
    commands.spawn().insert(SecondComponent::StateA);
    commands.spawn().insert(SecondComponent::StateB);
    commands.spawn().insert(SecondComponent::StateA).insert(FirstComponent(3.0));
    commands.spawn().insert(SecondComponent::StateB).insert(FirstComponent(4.0));
}

#[derive(Debug)]
struct FirstComponent(f32);

#[derive(Debug)]
enum SecondComponent
{
    StateA,
    StateB
}

fn first(query: Query<&FirstComponent>)
{
    for entity in query.iter()
    {
        println!("First: {:?}", entity)
    }
}

fn first_no_second(query: Query<&FirstComponent, Without<SecondComponent>>)
{
    for entity in query.iter()
    {
        println!("First without Second: {:?}", entity)
    }
}

fn first_and_second(query: Query<&FirstComponent, With<SecondComponent>>)
{
    for entity in query.iter()
    {
        println!("First with Second: {:?}", entity)
    }
}

bevy 的 ecs 如何了解某个 Queue 需要启动哪些系统。在 World 中,组件在某种程度上与实体相关,对吗?是否有可能从外部以某种方式追踪这种联系?我真的很喜欢它的工作原理,对我来说它看起来很神奇,但我想了解“幕后”发生的事情

4

1 回答 1

1

我发现只有通过 AppBuilder 才能直接访问世界、组件和实体。下面是一个足以满足我的目的的示例代码

use bevy::prelude::*;
use bevy::ecs::component::{ComponentId, ComponentInfo};


fn main()
{
    App::build()
        .add_plugin(WorldPlugin::default())
        .run()
}

// some components for test
struct TestComponent;
struct TestComponent2;


#[derive(Default)]
struct WorldPlugin {}

impl Plugin for WorldPlugin
{
    fn build(&self, app: &mut AppBuilder)
    {
        // creating an entity with both test components
        let entity = app.world_mut()
            .spawn()
            .insert(TestComponent)
            .insert(TestComponent2)
            .id();
        // to interact with components and entities you need access to the world
        let world = app.world();
        // components are stored covertly as component id
        for component_id in get_components_ids(world, &entity).unwrap()
        {
            let component_info = component_id_to_component_info(world, component_id).unwrap();
            println!("{}", extract_component_name(component_info));
        }

    }
}


/// gets an iterator component id from the world corresponding to your entity
fn get_components_ids<'a>(world: &'a World, entity: &Entity) -> Option<impl Iterator<Item=ComponentId> + 'a>
{
    // components and entities are linked through archetypes
    for archetype in world.archetypes().iter()
    {
        if archetype.entities().contains(entity) { return Some(archetype.components()) }
    }
    None
}

fn component_id_to_component_info(world: &World, component_id: ComponentId) -> Option<&ComponentInfo>
{
    let components = world.components();
    components.get_info(component_id)
}

fn extract_component_name(component_info: &ComponentInfo) -> &str
{
    component_info.name()
}

此代码可以为您的实体获取一组组件,这使您可以获取有关组件的任何数据。这是从bevy源代码中获取的ComponentInfo结构的描述

#[derive(Debug)]
pub struct ComponentInfo {
    name: String,
    id: ComponentId,
    type_id: Option<TypeId>,
    // SAFETY: This must remain private. It must only be set to "true" if this component is
    // actually Send + Sync
    is_send_and_sync: bool,
    layout: Layout,
    drop: unsafe fn(*mut u8),
    storage_type: StorageType,
}

世界存储了一组组件、实体,并且“原型”用于链接它们。下面附上一个示例图。 简图

于 2021-05-13T20:14:04.187 回答