#How can I get a list of Components for a given Entity?

34 messages · Page 1 of 1 (latest)

lament solstice
#

I want to query for entities with a given component, I then want to loop through these entities and do stuff to them based on their other components. Is there a way to then loop through or check for various components after the initial query?

outer smelt
lament solstice
#

@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.

outer smelt
#

Oh hm, I think I'm misremembering

#

But there's probably a better way nowadays

lament solstice
#

🤔

outer smelt
#

Oh wait yeah I marked that as the answer even lol

#

That should get you a Vec of ComponentInfo

lament solstice
#

world.inspect_entity(ent) this is probably good for me.. just didn't know it existed

#

thanks for working with me to find this.

outer smelt
#

No problem!

lament solstice
#

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

pine swallow
#

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

surreal yew
#

Or use a paramset right?

pine swallow
#

That works yeah :p I've never used them personally so I kinda forgot lol

jagged light
lament solstice
#

@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.

lament solstice
lament solstice
jagged light
#

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>>,
) {
  // ...
}
lament solstice
outer smelt
#

You could also consider the Has query param instead of Option if all you need to know about is the component’s existence

lament solstice
#

i.e.

for-loop {
match (compA, compB, compC, compD, compE, compF, compG)
(Some(_), _, _, _, _, _, _,)
.. .. .
..  .. 
}
#

like this^

lament solstice
#

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?

outer smelt
#

It's more like Option<&T> than With<T>