Add const Entity::PLACEHOLDER
(#6761)
# Objective One of the use-cases for the function `Entity::from_raw` is creating placeholder entity ids, which are meant to be overwritten later. If we use a constant for this instead of `from_raw`, it is more ergonomic and self-documenting. ## Solution Add a constant that returns an entity ID with an index of `u32::MAX` and a generation of zero. Users are instructed to overwrite this value before using it.
This commit is contained in:
parent
2364a305c0
commit
416a33e613
@ -116,21 +116,8 @@ pub enum AllocAtWithoutReplacement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Entity {
|
impl Entity {
|
||||||
/// Creates a new entity reference with the specified `index` and a generation of 0.
|
/// An entity ID with a placeholder value. This may or may not correspond to an actual entity,
|
||||||
///
|
/// and should be overwritten by a new value before being used.
|
||||||
/// # Note
|
|
||||||
///
|
|
||||||
/// Spawning a specific `entity` value is __rarely the right choice__. Most apps should favor
|
|
||||||
/// [`Commands::spawn`](crate::system::Commands::spawn). This method should generally
|
|
||||||
/// only be used for sharing entities across apps, and only when they have a scheme
|
|
||||||
/// worked out to share an index space (which doesn't happen by default).
|
|
||||||
///
|
|
||||||
/// In general, one should not try to synchronize the ECS by attempting to ensure that
|
|
||||||
/// `Entity` lines up between instances, but instead insert a secondary identifier as
|
|
||||||
/// a component.
|
|
||||||
///
|
|
||||||
/// There are still some use cases where it might be appropriate to use this function
|
|
||||||
/// externally.
|
|
||||||
///
|
///
|
||||||
/// ## Examples
|
/// ## Examples
|
||||||
///
|
///
|
||||||
@ -138,8 +125,8 @@ impl Entity {
|
|||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # use bevy_ecs::prelude::*;
|
/// # use bevy_ecs::prelude::*;
|
||||||
/// // Create a new array of size 10 and initialize it with (invalid) entities.
|
/// // Create a new array of size 10 filled with invalid entity ids.
|
||||||
/// let mut entities: [Entity; 10] = [Entity::from_raw(0); 10];
|
/// let mut entities: [Entity; 10] = [Entity::PLACEHOLDER; 10];
|
||||||
///
|
///
|
||||||
/// // ... replace the entities with valid ones.
|
/// // ... replace the entities with valid ones.
|
||||||
/// ```
|
/// ```
|
||||||
@ -158,11 +145,25 @@ impl Entity {
|
|||||||
/// impl FromWorld for MyStruct {
|
/// impl FromWorld for MyStruct {
|
||||||
/// fn from_world(_world: &mut World) -> Self {
|
/// fn from_world(_world: &mut World) -> Self {
|
||||||
/// Self {
|
/// Self {
|
||||||
/// entity: Entity::from_raw(u32::MAX),
|
/// entity: Entity::PLACEHOLDER,
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
|
pub const PLACEHOLDER: Self = Self::from_raw(u32::MAX);
|
||||||
|
|
||||||
|
/// Creates a new entity ID with the specified `index` and a generation of 0.
|
||||||
|
///
|
||||||
|
/// # Note
|
||||||
|
///
|
||||||
|
/// Spawning a specific `entity` value is __rarely the right choice__. Most apps should favor
|
||||||
|
/// [`Commands::spawn`](crate::system::Commands::spawn). This method should generally
|
||||||
|
/// only be used for sharing entities across apps, and only when they have a scheme
|
||||||
|
/// worked out to share an index space (which doesn't happen by default).
|
||||||
|
///
|
||||||
|
/// In general, one should not try to synchronize the ECS by attempting to ensure that
|
||||||
|
/// `Entity` lines up between instances, but instead insert a secondary identifier as
|
||||||
|
/// a component.
|
||||||
pub const fn from_raw(index: u32) -> Entity {
|
pub const fn from_raw(index: u32) -> Entity {
|
||||||
Entity {
|
Entity {
|
||||||
index,
|
index,
|
||||||
|
@ -31,7 +31,7 @@ impl Parent {
|
|||||||
// better ways to handle cases like this.
|
// better ways to handle cases like this.
|
||||||
impl FromWorld for Parent {
|
impl FromWorld for Parent {
|
||||||
fn from_world(_world: &mut World) -> Self {
|
fn from_world(_world: &mut World) -> Self {
|
||||||
Parent(Entity::from_raw(u32::MAX))
|
Parent(Entity::PLACEHOLDER)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -543,7 +543,7 @@ pub fn queue_sprites(
|
|||||||
image_handle_id: HandleId::Id(Uuid::nil(), u64::MAX),
|
image_handle_id: HandleId::Id(Uuid::nil(), u64::MAX),
|
||||||
colored: false,
|
colored: false,
|
||||||
};
|
};
|
||||||
let mut current_batch_entity = Entity::from_raw(u32::MAX);
|
let mut current_batch_entity = Entity::PLACEHOLDER;
|
||||||
let mut current_image_size = Vec2::ZERO;
|
let mut current_image_size = Vec2::ZERO;
|
||||||
// Add a phase item for each sprite, and detect when succesive items can be batched.
|
// Add a phase item for each sprite, and detect when succesive items can be batched.
|
||||||
// Spawn an entity with a `SpriteBatch` component for each possible batch.
|
// Spawn an entity with a `SpriteBatch` component for each possible batch.
|
||||||
|
Loading…
Reference in New Issue
Block a user