Boxy
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me 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
    • Save as template
    • 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 Create Help
Create Create new note Create a note from template
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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me 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
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Library Relations ## Summary relations is a useful feature which previous was thought to require a bunch of sweeping changes to internals but can actually be implemented as "solely" a 3rd party library with a small addition to `bevy_ecs`. The only thing this relies on is `on_remove_from_despawn` described [here](https://hackmd.io/7npDczZqTdK2gqHV1q9Rfw?view#Hooks). ## Impl steps We have to make `Children` and `Parent` private types, this means that downstream crates cannot write `&Parent/Children` or `&mut Parent/Children`. This also forbids `.remove::<Parent/Children>` and `.insert::<Parent/Children>` which means our hierarchy cannot be mutated by downstream users. It is a bit useless of a hierarchy if users cannot immutably query for parent/children relationships so to allow that we introduce custom worldqueries that mimic the functionality of `&Parent`/`&Children`. The last thing we need to do is use `on_remove_from_despawn` to clean up `Parent`/`Children` components on other entities when an entity is despawned with any of those components. I.e. if `foo` is a parent of `bar` then if `foo` is despawned we should use the `on_remove_from_despawn` hook to remove the `Parent` component from `foo`. ## More complex impl previous designs for "library relations" relied on having `MutPerm` and `InsertHook`/`RemoveHook` which would allow for having `Parent`/`Children` be public types and making `.insert::<Parent>` Just Do The Right Thing. see [this](https://hackmd.io/7npDczZqTdK2gqHV1q9Rfw?view) for reading on those proposed features. ## Not Really Relations this doc mostly just describes "how to make parent/child robust" rather than a _general mechanism for relations_ but that can be done outside of bevy_ecs in a 3rd party crate (which is a really good thing because it takes a while for things to get merged into bevy and also rfcs suck and this is gonna take enough of them). It ought to be "fairly" simple to make a crate for this but not gonna design that in my head yet. for general purpose relations we want to store data "with" each relation and allow mutation of it. to allow this we can introduce a custom worldquery which acts like `&mut Children/Parent` but only gives access to the user data not the relation target that should be considered immutable. (note: for `Parent`/`Child` there is no data associated with each relation) --- Note: the rest of this doc is just leftoever from what was here before, I dont wanna remove it because i cant be bothered to read it all so idk if there is valuable stuff in here that we dont want to lose. It's not really intended to be read with the rest of this document but i can't stop you if you want to: ## Component Permissions "Library relations" are a nice abstraction on top of bevy. We need a way to provide components that can only be read by ?downstream?(expand) users, that is not mutated or (added or removed). Add associated types `AddRemovePerm` and `MutatePerm` to the `Component` trait to allow plugin authors to restrict how operations are able to be performed on their components such that it is possible to make it so downstream systems and plugins can ONLY read the component NOT add/remove/mutate. ```rust trait Component { ... // other stuff type AddRemovePerm; type MutatePerm; } ``` #### Sample Impl ```rust impl<T: Component<MutPerm = ()>> Mut<T> { fn deref_mut(&mut self) -> &mut T } impl<T: Component> Mut<T> { fn deref_mut_with_perm(&mut self, T::MutPerm) -> &mut T } ``` ?Significance of now having `#[derive(Component)]` now? This is enables us to define things like the `Children` and `Parent` components in such a way that it is impossible for users to sabotage the parent/child heirarchy by mutating or add/removing the components out of sync with eachother. We can implment this by changing the impl of DerefMut and the into_inner method on Mut to only work when `T: Component<Perm = ()>` where Perm is one of the two new associated types of Component, and add some other methods that take T::Perm. All existing code would continue to work perfectly. You will only notice a change if you use a plugin that has restricted some of their exposed `Component`'s Component::AddRemovePerm or MutatePerm associated type paramters. All current bevy code assumes no separation of mutabilty concerns, and with the default associated types being `()` this continues to work just as before. I think this idea is better than custom query accessors because things will just work™️ as you expect &Parent will work plus you dont need to go looking for docs on how to access &Parent it just works™️. Alice: "And unsurprisingly, this would be killer for indexes, assuming you want to do live updating" #### Simple impl process - add the two associated types to component trait - and then find every API that allows adding/removing components - and every API that lets you mutate a component - and make it work with T: Component<AddRemovePerm|MutatePerm = ()> instead of T: Component etc In usual *bevy* style, what we taketh internally we giveth to app/plugin authors. Users too can now define 'read-only' components, that are mutable within their plugin but not by external plugins. This according to @alice-i-cecile does good things for app/plugin composability. "This design would do very interesting things to plugin safety, FYI" "Good things. Plugins will be able to make internal intermediate types read-only but public, which is useful for debugging and triggering downstream effects" @therawmeatball "<shameless-plug>seems kinda relevant https://github.com/bevyengine/bevy/pull/2363</shameless-plug>, it lets you bypass change detection inside of your crate with similar token stuff without derive(Component). It lets you be notified if a user modifies your resource / component, but then lets you handle that modification without further triggering change detection" @boxy "we have to allow Query<&mut T> when MutatePerm is not () because it doesnt necessarily mean you cant mutate it, you could actually have that token, for example our Noitaler<T> likely has a MutatePerm = PrivateBevyType which it uses to change the parent" @therawmeatball and @boxy "I guess we could add another assoc type for like type BypassChangeDetectionPerm that could be neat so in that PR you just have a trait for T: CanBeGottenUntracked which is kind of similar to a T: Component<BypassChangeDetectionPerm = ()> i guess" @boxy "regardless I dont think AddRemovePerm or MutatePerm can use that way of doing things, so unifying them would mean that your BypassChangeDetection would have to change, rather than this stuff changing to be like that" "AddRemove is just a bound on the commands for add/remove (and on the direct world methods/entitymut)" "add/remove implies the ability to mutate mutate doesnt necessarily imply add/remove though so I think the distinction is useful we can add a CleanBundle trait becuase you can be sure that an entity still has a component there is likely a way yeah (almost certainly tbh) I dont think theres any way around the fact that a bundle with an AddRemovePerm not () will be a pain to use like, the bundle would have to end up with an addremoveperm which is just a tuple of all its fields' AddRemovePerms I feel like bundles not supporting stuf with ADdRemovePerm really isnt a big deal though a large amount of stuff with an ADdRemovePerm is likely going to have its add/removes encapsulated in a library instead of exposed" t's literally just subtly changing the add/remove graph on archetypes "it is not a big performance problem we can probably figure something out for this anyway even if the damn ADdRemovePerm token for a bundle is like (Field1::AddRemovePerm, Field2::AddRemovePerm, ... FieldN::AddRemovePerm)" right so Query doesnt care about any of this if we have a type with MutatePerm = Blaaaaa then Query<&mut T> would still be a valid type to use in your system and it'd still return a Mut<T> when iterating it's up to the author of the library/wahtever to choose how people get ahold of the tokens so for Noitaler as an example we'd make the token be a private type that is not exposed and have it be a ZST so we can just create it out of thin air that way only bevy can add/remove/mutate Noitaler<T> as we are the only ones who can create the token you'd get a compile error if you tried to deref a Mut<T> without the token because Mut<T> wouldnt implement DerefMut if T: Component<MutatePerm = ()> didnt hold I think that Children would also have AddRemovePerm and MutatePerm set to not-() because changing the contents of Children should go through commands so that we can update the RelationGraph<Children> when we add/remove the Children component we need to sync up with Noitaler and RelationGraph so that perm would have to be set to soemthing bevy-only and mutatng too so that people cant change the list of children without updating Noitaler and RelationGraph once we have the Perm stuff implemented, adding relations is just a matter of designing RelationGraph and then adding some Commands for manipulating collections we can figure that stuff out way later though I think, dont wanna get too far ahead 😅 cart and I have talked about it before so it's definitely doable so no point delaying the perm stuff to figure that out 🤔 ## Collections Trait

    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