#Storing selected entity
18 messages · Page 1 of 1 (latest)
You can store the Entity id for the entity you selected and then call the get method on a Query to get the query data for that entity.
Thanks
And how do I get Entity id after it is generated by a system? Should it emit an event?
After calling spawn on a Commands you can call .id() on the result to get the Entity id of the just spawned entity
Entity is also a valid parameter for queries, so you can e.g. do Query<(Entity, &Foo)> to get a reference to the Foo of all components that have one and also get their Entity id.
So something like this would be ok?
#[derive(Resource)]
pub struct CurrentSystem(Entity);
Yes that seems reasonable
Is it possible to execute a query inside a system?
What do you mean execute a query?
I want to recieve the entity in a system, and inside system's body execute a query to get entity's components
You normally get queries as parameters of systems
For example:
fn my_system(query: Query<&Foo>) {
// use query to get references to Foo components
}
Ah, ok, I'll request all necessary components and filter by entity (inside system's body) then
Don't "filter" by entity, use query.get(entity) to directly get the components data for that entity
This is much faster than going through all the entities
pub fn body_list_system(
***
maybe_system: Res<CurrentSystem>,
star_names: Query<&Name, With<Star>>,
planet_names: Query<&Name, With<Planet>>,
) {
if let Some(system) = maybe_system.0 {
let star_name = star_names.get(system).unwrap();
}
I have this code right now, how can I access elements, returned by planet_names query, that belong to children of system binding?
You'll have to query for the Children component of your system entity, then that will give you the Entity ids of its children