#Inserting an entity and resource in one system?

3 messages · Page 1 of 1 (latest)

glacial lance
#

Having a weird issue using command two times in a system

fn generate_map_button(
    mut commands: Commands,
    mut contexts: EguiContexts,
    mut gen_event_ew: EventWriter<GenEvent>,
    mut q_egui_graph: Query<&mut EguiGraph>,
    mut egui_map: Option<ResMut<EguiMap>>,
) {
    let ctx = contexts.ctx_mut();

    egui::SidePanel::left("left panel")
        .default_width(150.0)
        .show(ctx, |ui| {

            if ui.button("Generate Map").clicked() {
                let entity = commands.spawn(EguiGraph::default());

                let map_config = MapConfig { difficulty: 1 };

                gen_event_ew.send(GenEvent {
                    entity: entity.id(),
                    kind: GenKind::Map(map_config),
                });

                match egui_map {
                    Some(mut egui_map) => egui_map.0 = entity.id(),
                    None => commands.insert_resource(EguiMap(entity.id())),
                }
            }
        });
}

There's a second mutable borrow at commands.insert_resource()? Is it impossible to insert an entity and a resource in the same system?

vernal rivet
#

commands.spawn(...) returns an EntityCommands that borrows from commands, and you keep it alive until you do the entity.id() at the very end, which conflicts with the commands.insert_resource(...).
To solve this you can simply store the result of entity.id() just after calling commands.spawn(...) (you could even do let entity = commands.spawn(...).id();) and use that afterwards

modest eagle
#

let entity = commands.spawn(...).id();
plus