我正在使用军团ECS,并试图使用多个可变查询,并遇到一些借用检查器约束。
我基本上希望通过与所有其他组件进行比较来更新组件的状态。
本质上-如果组件与另一个组件相邻,则使其为蓝色。
let components = <&Component>::query()
.iter(world)
.collect();
<&Component>::query()
.iter_mut(world)
.for_each(|component| {
// for each c in components
// make component blue if c is adjacent to this component
})以上错误为cannot borrow *ecs as mutable more than once at a time
|
128 | .iter(&ecs)
| ---- immutable borrow occurs here
...
134 | .iter_mut(&mut ecs)
| ^^^^^^^^ mutable borrow occurs here
135 | .for_each(|(_, fov)| {
136 | let fovs: HashSet<Point> = components
| --- immutable borrow later captured here by closure还有别的方法可以做到这一点吗?我认为克隆最初的集合会使我与World/ecs脱钩。但是,即使有了克隆,迭代组件集合也是不可变的借用。
发布于 2022-01-20 15:26:52
问题是您的components向量是一个包含对Component的引用的向量,它借用了world,这意味着您不能在以后编辑组件时随意借用它。
您可以通过几种方法解决这一问题:
compontents向量时克隆组件。很简单,但一开始需要更多的allocation.CommandBuffer over Entities,可能会使用组件过滤器(如果这样可以锁定其筛选所使用的存储),并使用world.entry[_mut]一次访问一个组件。不需要分配,但可能有更糟的performance.https://stackoverflow.com/questions/70779773
复制相似问题