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

Semi-automatic registration #52

Open
ItzShiney opened this issue Jan 2, 2024 · 4 comments
Open

Semi-automatic registration #52

ItzShiney opened this issue Jan 2, 2024 · 4 comments
Assignees

Comments

@ItzShiney
Copy link

ItzShiney commented Jan 2, 2024

It is possible to make struct registration semi-automatic, using inventory crate. The solution simplifies registration a lot and does not require a centralized registration function with all the structs listed.

A feature (which may not be enabled by default) can be introduced to toggle this, since it requires an additional dependency which may not be wanted.

It can be achieved using these three things:

  1. A helper struct:
pub struct Register(pub fn(&mut World));
  1. An attribute #[register] for impl Trait for Struct, which generates:
inventory::submit!(bevy_trait_query::Register(|world| {
    use bevy_trait_query::RegisterExt;
    world.register_component_as::<dyn Trait, Struct>();
}));
  1. A global registration function, which should be called manually (that's why the solution is semi-automatic):
fn register_traits(world: &mut World) {
    inventory::collect!(Register);
    for callback in inventory::iter::<Register> {
        callback(world);
    }
}
@joseph-gio joseph-gio self-assigned this Jan 3, 2024
@RobWalt
Copy link
Collaborator

RobWalt commented Nov 2, 2024

I think we can look into that. As far as I'm aware, inventory isn't supported on WASM though, so this would probably be feature gated. Nice find!

See bevyengine/bevy#3936 for a reference in upstream, links and discussion on the topic on a broader range

@joseph-gio
Copy link
Owner

Another option I looked into a while ago is moving trait registration into a method on the Component trait. This would still require you to manually register each trait for each component type (likely using an attribute on the component derive macro) but we could make it a compile-time error if you forget to do so.

When I tried this I ran into issues because currently we rely on all trait components being registered at app startup, whereas with the approach I'm describing each component would only be registered the first time it's used in the World. There's likely a way to work around this, though

I'm on my phone rn but I can go into more detail later if needed

@RobWalt
Copy link
Collaborator

RobWalt commented Nov 5, 2024

Uhhh, sounds jnteresting as well. I would be interested. But it would need to happen upstream, right? Anyways, I'd probably even try to make it happen there.

@joseph-gio
Copy link
Owner

joseph-gio commented Nov 6, 2024

We'd need to add a default method to the Component trait and then add a bevy-trait-query::Component derive macro that would implement it. Its a very small upstream change and the method doesn't need to be for trait queries specifically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants