bevyengine / bevyengine/bevy

Non-unique resource

Open
#10,789 0 comments 0 reactions 0 assignees View on GitHub
A-ECS C-Feature X-Needs-SME
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## What problem does this solve or what need does it fill?

Consider generic code connecting two systems:

```rust
// Pseudocode
fn connect(a: impl System, b: impl System) -> (impl System, impl System) {
let resource = ???
let a = a.pipe(|v| resource.write(v));
let b = (|| resource.read()).pipe(b);
(a, b)
}
```

Currently it is not trivial to implement, because as far as I understand there's no API to allocate such resource. Such resource can be allocated outside of bevy, and some mutex can be used to make access safe.

But it would be nicer if there was some API in Bevy to implement it.

## What solution would you like?

Public API:

```rust
#[derive(Clone, Copy)]
struct NonUniqueResourceRef { ... }

impl World {
fn new_non_unique_resource(&mut self) -> NonUniqueResourceRef { ... }
}

impl NonUniqueResourceRef {
// System that writes to the resource. Tells the schedule it requires unique access to this resource instance (not all of T)
fn write_system(&self) -> impl System { ... }

fn read_system(&self) -> impl System { ... }
}
```

Internally it is implemented as:

```rust
struct NonUniqueResourceRef {
component_id: ComponentId, // Unique per NonUniqueResourceRef
index: usize, // Index in table per T
archetype_component_id: ArchetypeComponentId, // to allow mutable access to T with different indices
}

struct Storages {
...
non_unique_resources: SparseSet,
}
```

## What alternative(s) have you considered?

* Use resource `Vec` and index, but that reduces parallelism
* Allocate outside of bevy, use mutex for safety, synchronize access using before/after
* pass unique type marker to each call to `connect` allocate regular resource

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the World and Storages APIs, along with the System access and scheduling behavior described in the issue. Compare the proposed NonUniqueResourceRef, ComponentId, index, ArchetypeComponentId, and non_unique_resources storage design with existing Bevy patterns. Done means there is an agreed public API that supports distinct resource instances while preserving safe access and scheduling.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.