Skip to content

bsn! if/else blocks MVP ⚖️#24900

Open
ItsDoot wants to merge 2 commits into
bevyengine:mainfrom
ItsDoot:bsn/if
Open

bsn! if/else blocks MVP ⚖️#24900
ItsDoot wants to merge 2 commits into
bevyengine:mainfrom
ItsDoot:bsn/if

Conversation

@ItsDoot

@ItsDoot ItsDoot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Objective

Ergonomic conditional component insertion has long been a sought-after capability in Bevy:

With the introduction of scenes and the bsn! macro in #23413, we finally have a good pathway to that.

Solution

Note for reviewers: I recommend reviewing commit-by-commit. The first commit does some infrastructural setup for the main feature commit.

This PR adds support for conditional patching:

bsn! {
    if is_boss {
        Health { current: 500, max: 500 }
    } else { // else blocks are optional
        Health { current: 100, max: 100 }
    }
}

as well as conditional scene inclusion:

bsn! {
    if is_boss {
        dragon()
    } else {
        Children [
            MoonlightGreatsword,
            PlateArmor
        ]
    }
}

However, due to the complex nature of their implementation, as well as the implementation of bsn in 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/else block, 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 macro code 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:

  • You wouldn't be able to use if let in a mixed conditional because the condition would be hoisted outside the generated scene itself.
  • You wouldn't be able to access the (currently undocumented) _context variable 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

bsn! {
    // conditional patching
    if is_boss {
        Health { current: 500, max: 500 }
    } else { // else blocks are optional
        Health { current: 100, max: 100 }
    }
    
    // conditional scene inclusion
    if is_dragon {
        dragon()
    }

    // conditional children (or any other relation type)
    if is_dragon {
        Children [
            DragonRider
        ]
    }
}

@ItsDoot ItsDoot added C-Feature A new feature, making something new possible A-Scenes Composing and serializing ECS objects D-Complex Quite challenging from either a design or technical perspective. Ask for help! M-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward D-Macros Code that generates Rust code labels Jul 7, 2026
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
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.

@JMS55 JMS55 requested a review from cart July 7, 2026 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Scenes Composing and serializing ECS objects C-Feature A new feature, making something new possible D-Complex Quite challenging from either a design or technical perspective. Ask for help! D-Macros Code that generates Rust code M-Release-Note Work that should be called out in the blog due to impact S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants