#How can I get a list of Components for a given Entity?
34 messages · Page 1 of 1 (latest)
Do you just need their info? If so you can add &Components to your system and use https://docs.rs/bevy/latest/bevy/ecs/component/struct.Components.html#method.iter
Stores metadata associated with each kind of Component in a given World.
@outer smelt I can't get any of my queries to work, could you give me an example with &Components?
Or are you saying to use world: &mut World, parameter with .components()?
It's not clear to me how something world.components().component_id::<SomeCompA>() relates to specific entity in a Query.
Oh hm, I think I'm misremembering
I was thinking about something like this: https://github.com/bevyengine/bevy/discussions/3332#discussioncomment-1809893
But there's probably a better way nowadays
Stores and exposes operations on entities, components, resources, and their associated metadata.
🤔
Oh wait yeah I marked that as the answer even lol
That should get you a Vec of ComponentInfo
world.inspect_entity(ent) this is probably good for me.. just didn't know it existed
thanks for working with me to find this.
No problem!
hmm.. i still can't figure out how to use world.. lol.. either getting a &World conflicts with a previous mutable system parameter. Allowing this would break Rust's mutability rules by putting it in the system params of the fn or I'm calling doing a let world = World::new(); in the fn but then after trying to use it, I'll get errors like Entity 2446v0 does not exist
The world contains all the entity data and resources, so you can't reference it immutably while also referencing one of its inner parts mutably
Just make your system take only an &mut World instead, you can access any resources or components you need through the world directly
Or use a paramset right?
That works yeah :p I've never used them personally so I kinda forgot lol
You might consider using something like
fn my_system(
query: Query<&ComponentA, Option<&ComponentB>, Option<&ComponentC>>
) {
// ...
}
instead
@surreal yew i would need a very complicated/long param set. the limit for param sets is 7 or 8 queries and I don't know how to nest them. .. so i didn't consider this route.
so if I combine an Entity in with this query I just can mark like 20 components as optional and then in the system I just check for None or Some on each entity within a loop?
Yep
never considered this.. let me see if it works for what im trying to do.
You could also consider refactoring into multiple systems
fn sword_system(
swords: Query<(Entity, &Stats), With<Sword>>,
) {
// ...
}
fn rifle_system(
rifles: Query<(Entity, &Stats), With<Rifle>>,
) {
// ...
}
that would overly complicate how many systems i would need .. im using your approach.. with a match statement
You could also consider the Has query param instead of Option if all you need to know about is the component’s existence
Returns a bool that describes if an entity has the component T.
i.e.
for-loop {
match (compA, compB, compC, compD, compE, compF, compG)
(Some(_), _, _, _, _, _, _,)
.. .. .
.. ..
}
like this^
I've tried using Has in the past.. i never found it to work ..
or at least the way I would expect it to work
and still don't understand how that's different than With<Comp>
just simplifies the disjoint query thing?
It's more like Option<&T> than With<T>