Skip to content

Basic concept for customizing Component reference type#7499

Draft
chrisjuchem wants to merge 6 commits intobevyengine:mainfrom
chrisjuchem:juchem/component-refs
Draft

Basic concept for customizing Component reference type#7499
chrisjuchem wants to merge 6 commits intobevyengine:mainfrom
chrisjuchem:juchem/component-refs

Conversation

@chrisjuchem
Copy link
Contributor

Looking for general feedback on this idea, and help solving a lifetime error:

error: lifetime may not live long enough
   --> crates/bevy_ecs/src/query/fetch.rs:535:9
    |
534 |     fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
    |               ------           ------- lifetime `'wshort` defined here
    |               |
    |               lifetime `'wlong` defined here
535 |         item
    |         ^^^^ associated function was supposed to return data with lifetime `'wlong` but it is returning data with lifetime `'wshort`
    |
    = help: consider adding the following bound: `'wshort: 'wlong`

Objective

Allow users to specify which reference types they want their components to be wrapped in when used in a Query. This would allow:

  1. Disabling change detection, by specifying &T and &mut T instead of &T (or Ref<T>) and Mut<T>.
  2. Enabling https://github.com/chrisjuchem/bevy_mod_index to update when components are changes by providing new Indexed reference types.

Solution

  • Add an associated type to components through which the desired reference types can be accessed and used when creating WorldQuery::Items
  • (todo) Allow setting this type with a new attribute in the component derive macro

Changelog

TODO

Migration Guide

TODO

@chrisjuchem chrisjuchem changed the title Basic concept Basic concept for customizing Component reference type Feb 4, 2023
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events X-Needs-SME This type of work requires an SME to approve it. labels Feb 4, 2023
@alice-i-cecile
Copy link
Member

I quite like the basic idea: I can definitely see how this would be useful for #4513. This could also actually be used to make "read-only" component types, by only ever returning a &/Ref.

@Guvante
Copy link
Contributor

Guvante commented Feb 4, 2023

I would take a look at #7344 and #6659 for some other work around allowing mutating the types here.

I believe the solve that #6659 did that I stole was to move the shrink function into a trait.

@chrisjuchem
Copy link
Contributor Author

Thanks @Guvante, that was the trick I needed.

@BenjaminBrienen
Copy link
Contributor

@chrisjuchem are you still working on this?

@BenjaminBrienen BenjaminBrienen added D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jan 5, 2025
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-Feature A new feature, making something new possible D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Needs-SME This type of work requires an SME to approve it.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants