bsn! if/else blocks MVP ⚖️#24900
Open
ItsDoot wants to merge 2 commits into
Open
Conversation
Contributor
|
It looks like your PR has been selected for a highlight in the next release blog post, but you didn't provide a release note. Please review the instructions for writing release notes, then expand or revise the content in the release notes directory to showcase your changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Objective
Ergonomic conditional component insertion has long been a sought-after capability in Bevy:
Optionbundles andBox<dyn Bundle>#19491With the introduction of scenes and the
bsn!macro in #23413, we finally have a good pathway to that.Solution
This PR adds support for conditional patching:
as well as conditional scene inclusion:
However, due to the complex nature of their implementation, as well as the implementation of
bsnin general, there are some tradeoffs to be aware of, and some concessions made as an MVP.A tale of tradeoffs
This PR is already a bit chunky, so to avoid further complexity I've reduced it to a minimal but still useful feature set. That means:
You cannot mix conditional patching and conditional scene inclusion within the same
if/elseblock, be that in the same branch or in separate branches.The macro makes patching and scene inclusion look the same in code, but they work very differently under the hood. I recommend using the
Inline macrocode action in VSCode (or other IDEs) to see what's generated.This is something we could support in a follow up, but even then it would have the following caveats:
if letin a mixed conditional because the condition would be hoisted outside the generated scene itself._contextvariable for templating context, whereas you can currently when conditionally patching.There is not a (documented) way to easily access templating context for conditional patching.
I think the discussion on how to surface that in the macro syntax will lead to some bike-shedding, so I'd like to avoid that here.
IDE autocomplete-ability was not a core focus of mine
If you have any ideas for improving that, let me know! If it's too much work we can save it for a follow up PR.
Testing
Added macro syntax tests, as well as tests for generated scene outputs. If you have additional test case recommendations, let me know!
Showcase