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

Query by DynamicBundle #360

Closed
cjhowedev opened this issue Dec 21, 2023 · 3 comments
Closed

Query by DynamicBundle #360

cjhowedev opened this issue Dec 21, 2023 · 3 comments

Comments

@cjhowedev
Copy link

cjhowedev commented Dec 21, 2023

Allow for issuing queries using DynamicBundle. We currently have to use a separate struct with #[derive(Query)]. It would be nice if we could query a bundle, for example:

use hecs::{World};

let mut world = World::new();
let mut target_world = World::new();

#[derive(Bundle, DynamicBundleClone, BundleQuery)]
struct MyBundle {
  integer: i32,
  float: f32,
}

let bundle_to_spawn = MyBundle { integer: 1, float: 2.0 };
let spawned = world.spawn(bundle);

for (entity, bundle) in world.query_mut::<MyBundleQuery>() {
  assert_eq!(entity, spawned);
  assert_eq!(bundle, bundle_to_spawn);
  target_world.spawn(bundle.clone())
}

Presumably, MyBundleQuery is generated by the #[derive(BundleQuery)]. This is necessary because each field needs to be wrapped in a reference to work as a query type, so we can't use the original MyBundle.

@cjhowedev
Copy link
Author

After some thought, this is externally implementable. Nevermind

@Ralith
Copy link
Owner

Ralith commented Dec 21, 2023

This has come up a few times before. The tricky part is that you might reasonably want either shared or unique references for each component of a bundle, so a clean general-purpose solution becomes complex. You could bodge something ugly but serviceable pretty easy with a declarative macro though, yeah.

@Ralith
Copy link
Owner

Ralith commented Dec 21, 2023

If your goal is actually just to do some asserts like that, you might be interested in #354.

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

2 participants