Bevy Developer Network
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    2
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # It's All Components and Entities?? `flecs` treats virtually all of data used by the ECS in a single homogenous way: as components on entities. In Bevy terms, this means: - the row-ish identifier is always `Entity` - the column-ish identifier is always `ComponentId` - no `bevy_ecs` code uses `TypeId`: it's all `ComponentId` (except when generating `ComponentId` for Rust types) - each resource is an entity, with its data stored in a component - each system is stored as an entity, with metadata and system state on components - ordering constraints are stored as relations between systems - executors query for entities and then run them - metadata for each component, resource and event type is stored in the ECS, with each type getting its own entity This path is appealing to many contributors and users of Bevy because: - new features (hooks, observers, relations, debugging tools...) just work on all of these different types of data, without needing reimplementation - less complexity, less maintenance burden, lower compile times - the existing powerful ECS features can be used to build more ECS features faster and easier - because we're using these ECS features to build more ECS features contributors will think of ways to improve them for all users That's a compelling argument! But there are some drawbacks: - new users may be confused about the different concepts and how they relate - it may become harder to understand our APIs by reading type signatures - semantic errors (e.g. passing a resource into a query) may not be enforced by the compiler, hurting new users and making refactors more painful - we may lose performance due to the increased generality - e.g. we can't assume at the type level that we only have one resource ## Rules Let's see if we can come up with some rules for these changes that mitigate the drawbacks. ### Rule 0: different concepts get different names If two things are used in different ways, they need distinct names, and documentation needs to consistently refer to them as distinct. The fact that "resources are implemented using entities and components" or "each system is stored as an entity" is something we should explain to advanced users: not hide. But that doesn't mean "resources are entities (or components)" or "systems are entities"! Advanced features are *built out of* simple ones. ### Rule 1: trait bounds must be semantically correct If `trait NewEcsDataType: Component`, users should be able to interact with it as a component in every meaningful way, and things should Just Work. This trait bound implies that "all NewEcsDataTypes are also components": we need to keep this promise to users. ### Rule 2: Strongly typed common APIs, weakly typed internals While strongly typed APIs and restrictive trait bounds are great for new users and refactoring, they can be quite limiting, both for power users and for dynamic types. Most users and call sites should use strongly typed APIs that take real Rust traits with unambigious names and bounds added for correctness. These should be what we teach, and preferred wherever we don't explicitly need to be generic. Under the hood though, these APIs should call (public!) untyped APIs to do the actual operation. These will accept and return `ComponentId` and `Entity`, which the higher level API will translate to / from the strongly typed forms. This means that the deepest internals of the ECS should not have trait bounds, use `TypeId` or require that any of the data represent actual Rust types. ### Rule 3: newtype identifiers, but provide escape hatches Whenever possible, high level APIs should provide and use wrappers over both `Entity` and `ComponentId`. These should be stored, accepted as arguments and returned wherever possible. However, these should be simple tuple structs with public fields, allowing users to extend the existing functionality with ECS-backed features of their choice. These newtypes help communicate intent and prevent confusing errors without restricting users or obscuring the implementation. ### Rule 4: ECS internals should be hard to accidentally interact with Interacting with the ECS internals can have surprising, difficult to debug, and destructive results! Messing with component metadata, despawning systems, resetting system state: all extremely confusing for users! While we *could* simply make them all inaccessible, power users will rightfully complain. Instead, they should be public, but impossible to *accidentally* hit. ## Groundwork We may need some additional tools to make these unifications viable. ### Groundwork 0: relations Many of the constraints that we want to model (schedules, system sets, system ordering constraints) are graph or tag-like. We need relations to make this migration not suck. ## Cleanup Some existing parts of Bevy violate these rules! ### Cleanup 0: remove `Event: Component` Unlike a hypothetical `Resource: Component` bound, this doesn't map to users' intuitions properly. Remove it in https://github.com/bevyengine/bevy/pull/17333 ### Cleanup 1: split apart observer and queue-based events These operate in extremely different ways, and using the same name and trait makes teaching and talking about these event-flavored constructs very hard. ## Unification Finally, on to the meat of things. There will surely be ### Unification 0: resources as entities Many features like hooks and observers work on resources, but not components. This is very annoying! This should be a nearly full unification: the concepts and behavior will be distinct, but resource data will transparently be component data to users. Details: - each resource gets its own `Entity` - resource data `T` is stored as a raw `T` component - each resource entity has a marker types `IsResource` and `ResourceEntity<T>` - the former is useful for inspectors and generic reasoning - the latter is useful for enforcing uniqueness - `ResourceEntity<T>` has an `OnInsert` hook that despawns any other existing `ResourceEntity<T>` entities, preserving global uniqueness - dynamic resources get a `DynamicResourceEntity` component that uses a more expensive scan (or relies on an index) - `trait Resource: Component`: everything that you can do with a component you should be able to do with a resource, but the reverse is not true - queries operate on resources by default (no default query filters here) - in most cases, this is a good default and allows users - `type Res<T: Resource> = Single<&T, With<ResourceEntity<T>>`, and equivalently for `ResMut` - painless migration! - still type-safe! This may cause complications with `NonSend` types: we should plan how we want to handle that. ### Unification 1: systems as entities We already store one-shot systems and observers as entities: ordinary systems should be entities too. The final implementation of this work will be relations-heavy, but we can move incrementally still. Schedules can store the metadata still, but point to system entities. ### Unification 2: queries as entities Store your queries as entities too and you can update them using hooks and observers. ### Unification 3: component and resource types as entities Rather than using a dedicated storage for this metadata, we can move it into the ECS proper. Each component type should have its own `ComponentInfo` entity. Resources should refer back to these same entities, rather than storing it on the resource entity itself, to avoid it being despawned when removing the resource. These are dangerous enough that they should be excluded via default query filters. This work also enables us to further unify `ComponentId` to store an `Entity`. As explained in Rule 3, this should be a newtype, not a removal of `ComponentId`.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully