Resources v2 #1186
Replies: 6 comments 18 replies
-
Similar but Distinct Syntax and Behavior between Resources and ComponentsThere are three facets to this:
For 2, when working with a single component we have:
But for a resource, the equivalent looks like:
This proliferation of wrapper types is confusing, especially to beginners, and limits our possible directions to expand. For 3. systems are automatically skipped by the scheduler if at least one of their Proposal: Use a filtering syntax for ResourcesBorrowing directly from the API for components, replace the existing syntax by adding a second optional type parameter:
Benefits:
Costs:
Proposal: Always run ChangedRes systemsBenefits:
Costs:
|
Beta Was this translation helpful? Give feedback.
-
Unclear How to Extend Behavior of Resources when Variants are NeededIt's common to begin writing a game using a true singleton, but then find that you need to extend the behavior to include multiple copies. For example, you might begin with a single With the current patterns, refactoring for this is painful. As I see it, the obvious options are:
Proposal: Resource Variants through Generics as Idiomatic BevyBut there's a better way! As shown here, you can use generic type arguments on the resources that you define in order to allow the creation of resource variants differentiated by their type, which can be seamlessly called and created using Bevy. This results in particularly elegant code (once initialization is set up), and becomes exceptionally powerful once const generics arrive, allowing you to do things like specifying To make this change, I think we want:
|
Beta Was this translation helpful? Give feedback.
-
"Thread-local resources" and "thread-local systems" are kind of a messAs I understand it, the current "thread-local resources" cannot be sent safely across threads, because they lack On the face of it, that seems reasonable: you handle thread-local resources in thread-local systems and don't worry about multithreading them. There are a few problems though:
Proposal: allow regular systems to access thread-local resourcesThis will require some scheduler work, but should be perfectly feasible. As long as we stick the system that requires a thread-local resource on the same thread as the resource in question, everything works just fine, as the data itself never has to cross the thread boundary. By default, we should stick all thread-local resources on the same thread to allow systems to access multiple of them at once. Rename "thread-local systems" to "global systems"Much better clarity, and avoids false connection to thread-local resources. Proposal: Design an API to create and access a copy of a resource for each active threadThis has a few steps:
Down the road, I can see a lot of use for this in accumulating events (and commands!) across disparate systems in a nonblocking way, eventually aggregating them in a later system. |
Beta Was this translation helpful? Give feedback.
-
System-local resources lack a proper name in the code and documentationThis is a simple problem: These are rare and strange enough that I think the clarity gained from calling the type |
Beta Was this translation helpful? Give feedback.
-
FromResources cleanup
|
Beta Was this translation helpful? Give feedback.
-
I know this is ancient history, but sometimes understanding open source code depends on understanding its history. I have been trying to understand the reason that both Is the distinction between FWIW, I would also have picked |
Beta Was this translation helpful? Give feedback.
-
Resources in Bevy are fantastically useful global singletons, but there are some strange warts in how they currently work. In the next few posts on this thread, I'm going to go over a few of the problems, and how I propose we fix them.
TL;DR:
Local
resources a name: "system-local resources".FromResources
for clarity.Inconsistent, Confusing API for Initialization
Right now, there are many ways to set up a resource:
init_resource
: Adds a new resource of the type set, with default values set viaFromResources
.add_resource
: Adds a new resource.insert_resource
: The same as 2, but viaCommands
rather than in theAppBuilder
.Local
: Automatically creates a new resource of the appropriate type if it doesn't exist, but that can only be seen by the calling system.add_event
] (https://docs.rs/bevy/0.4.0/bevy/app/struct.AppBuilder.html#method.add_event): Sets up the resource for you, and then adds a nice little system to handle cleanup for you.send + sync
on those resources.All of these other than 4 overwrite existing values for the events.
Proposal: Condense Resource Initialization Options
One of the stated goals of Bevy is that there should be one, good way to accomplish each desired behavior. To that end, I recommend the following changes to the API:
add_resource
andinsert_resource
toset_resource
: This makes it much clearer that these operations overwrite any existing values, and uses the same name for the same behavior.init_resource
in favor of implicit auto-initialization: This reduces boilerplate, avoids accidentally overwriting values due to AppBuilder method ordering, and makes the behavior consistent with system-local resources. This is the most controversial proposal: it's fairly magic, and will need to be tested to ensure that appropriate solutions still exist for foreign types that don't impl Default.Beta Was this translation helpful? Give feedback.
All reactions