#Storing selected entity

18 messages · Page 1 of 1 (latest)

odd folio
#

I have potentially a lot of entities with the same components, I need to select one of them (for displaying on the screen)
What is the best way to do this, such that is it easy to select a particular entity, store it and access the selected entity in systems

vital bridge
#

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.

odd folio
vital bridge
#

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.

odd folio
#

So something like this would be ok?

#[derive(Resource)]
pub struct CurrentSystem(Entity);
vital bridge
#

Yes that seems reasonable

odd folio
#

Is it possible to execute a query inside a system?

vital bridge
#

What do you mean execute a query?

odd folio
vital bridge
#

You normally get queries as parameters of systems

#

For example:

fn my_system(query: Query<&Foo>) {
    // use query to get references to Foo components
}
odd folio
#

Ah, ok, I'll request all necessary components and filter by entity (inside system's body) then

vital bridge
#

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

odd folio
#
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?

vital bridge