-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NonUniqueResource #10793
base: main
Are you sure you want to change the base?
NonUniqueResource #10793
Conversation
4990dcf
to
568d7b0
Compare
I don't understand how this could be used, or why it's a useful addition. Questions:
|
568d7b0
to
d3f3d66
Compare
Non-unique by
Exploring various options for implementing issue described in #8857. First code snippet in this PR description. It cannot use resource, because multiple pair of systems can use the same passed value type.
For example, for binding Bevy to dynamically typed language like Python. All "resources" will be the same
Resource can mainly be accessed through systems exposed as functions on the (We can also add direct accessors from |
I completely forgot why I wanted to add it in the first place. Another use case. This utility can also be used as replacement for conditions. Currently conditions can be configured only when While implementing the approach from linkd comment I found current condition API does not allow this automatic system linking with conditions, because So the idea isEach new condition (or non-builtin condition) is a system which writes to a Each Using proposed API, these custom conditions can be implemented like this:
(CC #10757 for Ideally I'd want to convert conditions to normal systems. Remove conditions builtin from |
First: I think should have been presented with some clearer motivation, expressed up front (either in the issue or in the pr, ideally both). I was in the process of writing this off in my head when it finally clicked. The motivation here: is "being able express value producer and consumer graphs in the schedule, where the 'value' is a unique piece of data that the scheduler can reason about". This does seem like an interesting (and likely useful concept), but before adding a new "foundational building block", I think we need a bit more holistic discussion about:
I think this is a good candidate for an RFC. I also have some concerns about this implementation, such as storing the table row directly in the NonUniqueResourceRef, as that would mean we could never clean up NonUniqueResources. But that something to discuss after (1) and (2) are answered / we have buy-in from ECS SMEs. |
My bad. I'm thinking about it this way. It is lower level API not meant to be used directly, but can be used to implement higher level primitives:
I'll try to write one if that's what you suggest.
This is meant to be low level API, higher level utilities can be built upon. |
Thanks :) There's a good template; please submit it to https://github.com/bevyengine/rfcs |
One more potential application of non-unique resources: fix concurrency of All Instead, the design could be this:
For simplicity let's assume that // current "Events"
struct EventsShard<T> {
events: Vec<T>,
}
// New events: just stores the list of shards.
pub struct Events<T> {
shards: Vec<NonUniqueResourceRef<EventsShard<T>>>,
}
pub struct EventWriter<T> {
shard: NonUniqueResourceRef<EventsShard<T>>,
}
impl SystemParam for EventWriter<T> {
type State = NonUniqueResourceRef<EventsShard<T>>;
fn init_state(world) -> Self::State {
let shard = world.new_non_unique_resource::<EventsShard<T>>();
let events = world.init_resource::<Events<T>>();
events.shards.push(shard);
shard
}
fn new_archetype(state) {
// I want exclusive access to my shard
}
}
pub struct EventWriter<T> {
shard: Vec<NonUniqueResourceRef<EventsShard<T>>>,
}
... It is a bit more complicated than this (all the readers must be initialized after all the writers). But the issue is that currently Bevy lacks basic building blocks to solve this issue (and other similar issues). This is not something can be worked around in third-party library or some undocumented API. The problem is: shared/exclusive access to anything can be only controlled by types in current Bevy. |
WIP implementation of #10789.
Objective
Consider generic code connecting two systems:
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:
TODO
(Edit: I think it is not possible).read_shared_system()
(return&T
).read_opt_system()
(returnOption<T>
,None
if resource was not written).read_opt_shared_system()
(returnOption<&T>
if resource was not written).remove_system()
(reset toNone
).read_copy_opt_system()
.read_copy_system()
Help
CC @ZBemo #8857