aevyrie / bevy_mod_raycast

A little mesh raycasting plugin for Bevy
https://crates.io/crates/bevy_mod_raycast
MIT License
304 stars 92 forks source link

conflict when accessing ResMut<Assets<Mesh>> #106

Closed tricarbonate closed 6 months ago

tricarbonate commented 7 months ago

I have a ray cast system that spawns new meshes when the ray hits certain objects. But the Raycast accesses the meshes (with Res<Assets>), so it is not possible to use ResMut<Assets> in the same system because it would cause error B0002 (see https://bevyengine.org/learn/errors/#b0002)

I have a system that looks like this:

pub fn chunk_deform_system(
    mut proc_entities: ResMut<ProcEntities>,
    camera: Query<&mut Transform, With<CameraComponent>>,
    mut raycast: Raycast, // meshes: &mut Assets<Mesh>,
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut time: ResMut<Time>,
    mut buttons: ResMut<Input<MouseButton>>,
) {

The access with mut meshes: ResMut<Assets> causes B0002 but I'm probably missing a better way of doing this

aevyrie commented 6 months ago

Yes, you need to read meshes to be able to raycast them, so you will need to split your system apart into smaller systems, or convert your mesh mutations into Commands.

My recommendation would be to split this up into smaller systems that do just one thing. E.g. a system that raycasts against things and either sends events, updates components, or updates a resource to track what things have been hit and the semantic significance of the hit (such as "this is a list of things hit by the players gun this frame"). You would then use this event/component/resource in the next system that processes this information.