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

bevy_ecs: Special-case Entity::PLACEHOLDER formatting #15839

Merged

Conversation

MrGVSV
Copy link
Member

@MrGVSV MrGVSV commented Oct 10, 2024

Objective

Oftentimes, users will store an entity on a component or resource. To make this component/resource Default-able, they might initialize it with Entity::PLACEHOLDER. This is sometimes done to avoid the need for an Option<Entity>, especially if it complicates other logic.

For example, it's used in this Selection resource to denote "no selection":

#[derive(Resource, Debug)]
struct Selection(Entity);

impl Default for Selection {
    fn default() -> Self {
        Self(Entity::PLACEHOLDER)
    }
}

The problem is that if we try to Debug the current Selection, we get back: 4294967295v1#8589934591. It's not immediately obvious whether or not the entity is an actual entity or the placeholder.

Now while it doesn't take long to realize that this is in fact just the value of Entity::PLACEHOLDER, it would be a lot clearer if this was made explicit, especially for these particular use cases.

Solution

This PR makes the Debug and Display impls for Entity return PLACEHOLDER for the Entity::PLACEHOLDER constant.

Feel free to bikeshed the actual value returned here. I think PLACEHOLDER on its own could work too. Swapped to PLACEHOLDER from Entity::PLACEHOLDER.

Testing

You can test locally by running:

cargo test --package bevy_ecs

Migration Guide

The Debug and Display impls for Entity now return PLACEHOLDER for the Entity::PLACEHOLDER constant. If you had any code relying on these values, you may need to account for this change.

@MrGVSV MrGVSV added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 10, 2024
@alice-i-cecile alice-i-cecile added the D-Straightforward Simple bug fixes and API improvements, docs, test and examples label Oct 10, 2024
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer just PLACEHOLDER.

@MrGVSV
Copy link
Member Author

MrGVSV commented Oct 10, 2024

Two votes for PLACEHOLDER so I swapped to that for now. If anyone has any other opinions, feel free to share!

@LiamGallagher737 LiamGallagher737 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Oct 11, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Oct 11, 2024
Merged via the queue into bevyengine:main with commit da4e776 Oct 11, 2024
27 checks passed
@MrGVSV MrGVSV deleted the mrgvsv/ecs/entity-placeholder-format branch October 11, 2024 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants