big_space icon indicating copy to clipboard operation
big_space copied to clipboard

How to access GridCommands via a Query?

Open motgenror opened this issue 11 months ago • 3 comments

Amazing crate, thank you. I'm struggling to figure out: after I spawned a new grid and some children in it, how do you come back to that grid and add more entities to it? Grid is a component and it can be queried for:

fn setup1(
    mut commands: Commands,
) {
    commands.spawn_big_space_default(|root_grid| {
        root_grid.insert(RootGrid);
        // add stuff to root_grid ...
    }
}
// ...
fn setup2(
    mut root_grid: Query<&mut Grid<GP>, With<RootGrid>>,
) {
    let mut root_grid = root_grid.single_mut();

    // ERROR: `root_grid` is not a `GridCommands`, so `spawn_spatial()` is not found for it
    root_grid.spawn_spatial((...));
}

I guess my question applies to any grid, not just the root one. Please help?

motgenror avatar Feb 17 '25 11:02 motgenror

Took me a while to find a manual workaround that the BigSpace's validator asks not to use 😉:

fn setup2(
    mut commands: Commands,
    root_grid: Query<(Entity, &Grid<GP>), With<RootGrid>>,
) {
    let (root_grid_id, root_grid) = root_grid.single();

    let object_pos = DVec3::new(/* position*/);
    let (object_cell, object_pos) = root_grid.translation_to_grid(object_pos);
    commands
        .spawn((
            object_cell,
            Transform::from_translation(object_pos),
        ))
        .set_parent(root_grid_id);
}

motgenror avatar Feb 18 '25 01:02 motgenror

The short answer is that this isn't implemented yet. Getting access to those commands is on my todo list!

aevyrie avatar Feb 21 '25 10:02 aevyrie

resolved in #52

jiangheng90 avatar May 09 '25 09:05 jiangheng90