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!
#Get component(s) for arbitrary entity
15 messages · Page 1 of 1 (latest)
You can do a query for collidable tiles and see if the target tile is in there
How do I create a new query inside a system?
If you need to get tile either way, you can query for, say, (&Tile, Option<&Collidable>) and see if the second field is Some
You don't, you put this query in the signature, along with any other queries
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 } } } }
Ah, you can put multiple queries in the system signature? I didn't know that!
Awesome!
Yeah, just make sure they don't conflict (like mutable ref and other ref to the same component), it'll panic
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
Ah yes. Because the player would also have a Collidable tag
So I would have to add something like query<(Collidable, TilePos), Without<Player>
either that, or you can get around it with SystemSet which prevents you from accessing the 2 queries at the same time