#Get component(s) for arbitrary entity

15 messages · Page 1 of 1 (latest)

hearty bloom
#

I'm making a roguelike dungeon crawler. I have a map where the tiles are entities, with compnents to restrict movement (e.g. Collidable). In my move system, I want to check whether the tile that my entity is moving to has any entities with a Collidable component. So I essentially need to run two queries in the system: one to get all things that can move and one to retrieve their target tile entities. What's the best way for me to do this? Thanks!

viscid haven
#

You can do a query for collidable tiles and see if the target tile is in there

hearty bloom
#

How do I create a new query inside a system?

viscid haven
#

If you need to get tile either way, you can query for, say, (&Tile, Option<&Collidable>) and see if the second field is Some

viscid haven
proper whale
#

something like this: fn s(mob_q: Query<...>, tile_q: Query<(&Tile, Option<&Collidable>, ...)) { for ... in mob_q.iter() { if let Ok((tile, maybe_collidable, ...)) = tile_q.get(...tile entity) { if let Some(collidable) = maybe_collidable { // the tile is collidable } } } }

hearty bloom
#

Ah, you can put multiple queries in the system signature? I didn't know that!

#

Awesome!

viscid haven
#

Yeah, just make sure they don't conflict (like mutable ref and other ref to the same component), it'll panic

hearty bloom
#

Ah, good tip!

#

Thanks very much!

viscid haven
#

You can have mutable ref and other ref to the same component type, as long as your queries never do it to the same actual component, like getting transforms of all objects in a room and getting mutable transform of player but you forgot player is an object in the room

hearty bloom
#

Ah yes. Because the player would also have a Collidable tag

#

So I would have to add something like query<(Collidable, TilePos), Without<Player>

deft dove
#

either that, or you can get around it with SystemSet which prevents you from accessing the 2 queries at the same time