Draft: Better Configuration/Settings #2143
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR contains an attempt at improving the state of our settings code to have a more central structure.
Atm this branch doesn't address the goals below beyond just cleaning up the current state of things a bit by moving some files around and refactoring a little. I figured we should discuss the way forward before I just implement something arbitrary.
Goals
To summarize some of the goals, I wrote the following at the top of the settings/mod.rs file which somewhat encapsulates where I'd like us to get:
Possible Directions to Explore
Here are some things I've considered in no particular order:
Bevy Reflect
One source of complexity for our current approach is that we use a derive macro to implement change application. This lets us easily implement these settings structs close to the code that uses them, but is pretty confusing to understand for new comers and in my experience kinda tough to update as you have to reason about what is available where.
The bevy_reflect crate might be a way out of this as it lets us dynamically iterate over the fields and structure of types. It also provides a useful primitive in Dynamic types which can be applied to other values. I envision this as being a useful for achieving the precedence rules. Each source of settings could produce a DynamicStruct which contains some subset of the fields in the final settings structs. Then when a given setting struct is requested, the DynamicStructs could be applied in order to a default setting list to get the final set.
I'm not yet sure how the bevy_reflect crate interacts with other attribute metadata. I believe we will still want to allow renaming of final settings or adjusting the user facing structure via attributes. I hope that bevy_reflect can support such a thing but I didn't see it in my first glance. Its possible we can get around that but im not positive.
Central Setting Tree
If we have a reflection based setting approach, maybe it makes sense to adjust our structure to use a single struct that contains substruct fields for each category of setting. Those sub structs could be defined near the code that uses it like we do today, but then its not just manually registering the types in a central hashmap.
Simplify Drastically
Our current settings approach is pretty complicated and does a lot of "magic". On the one hand that lets the code that uses it be pretty simple, but I also think its possible that the magic gets in the way of people understanding and contributing. Maybe theres a much simpler approach that defines the structure with a little more boilerplate but without playing derive macro tricks? That might be worth exploring as well.
What kind of change does this PR introduce?
Did this PR introduce a breaking change?
These changes will almost certainly result in some breaking changes, but if done correctly we should arrive at something that can last longer term.