gpt4 book ai didi

Bevy Game Engine 0.11.2 despawn function expectes a different parameter type(Bevy Game Engine 0.11.2 Despawn函数需要不同的参数类型)

转载 作者:bug小助手 更新时间:2023-10-25 12:04:08 26 4
gpt4 key购买 nike



I am trying to despawn some entitys I spawned using the commands.spawn() function but it is expecting a different type parament for the commands.entity().despawn function. Here is my code:

我正在尝试取消绘制我使用Commands.spawn()函数生成的一些实体,但它需要为Commands.Entity().despawn函数提供不同类型的参数。以下是我的代码:


fn despawn(
commands: Commands,
player: Query<&Player>,
) {
commands.entity(player).despawn()
}

this code gives me the error: expected struct bevy::prelude::Entity
found struct bevy::prelude::Query<'_, '_, &Player> How can I fix this?

此代码给出错误:Expect struct bevy::Prelude::Entity Found struct bevy::Prelude::Query<‘_,’_,&Player>如何修复此问题?


更多回答
优秀答案推荐

Commands::entity requires the Entity, which is an unique identifier for an entity and associated components. To get the Entity from a Query, include it in the parameters like shown here:

命令::Entity需要实体,实体是实体及其关联组件的唯一标识符。要从查询中获取实体,请将其包括在如下所示的参数中:


player: Query<(Entity, &Player)>,



A Query is an iterator-adjacent structure that yields the entities and components you're querying for. So your query can potentially yield multiple players if multiple entities have the Player component. So you can handle multiple of them like so:

查询是一个迭代器相邻的结构,它产生您要查询的实体和组件。因此,如果多个实体具有播放器组件,则您的查询可能会生成多个播放器。因此,您可以按如下方式处理多个问题:


fn despawn(
mut commands: Commands,
players: Query<(Entity, &Player)>,
) {
for (player_id, _player) in players.iter() {
commands.entity(player_id).despawn();
}
}

Or if only one entity is expected, you can use get_single:

或者,如果只需要一个实体,可以使用GET_SINGLE:


if let Ok((player_id, _player)) = players.get_single() {
commands.entity(player_id).despawn();
}



Lastly, if you don't actually need to access the Player component and have just included it to filter what entites are despawned, use a With filter instead. See disjoint queries for an example. It would look like this:

最后,如果您实际上不需要访问播放器组件,并且只包含了它来过滤被取消绘制的实体,那么可以使用With过滤器。有关示例,请参阅不相交查询。它看起来是这样的:


fn despawn(
mut commands: Commands,
players: Query<Entity, With<Player>>,
) {
for player_id in players.iter() {
commands.entity(player_id).despawn();
}
}

更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com