Skip to content

initial implementation of shared state for system params#22902

Draft
Person-93 wants to merge 19 commits intobevyengine:mainfrom
Person-93:feature/system-param-shared-state
Draft

initial implementation of shared state for system params#22902
Person-93 wants to merge 19 commits intobevyengine:mainfrom
Person-93:feature/system-param-shared-state

Conversation

@Person-93
Copy link
Copy Markdown
Contributor

Objective

Fixes #22885

Solution

I added associated function to SystemParam which returns all of the types that the param uses as shared state. It returns a static slice of SharedStateVTable which contains type-erased functions for working with the single instances of each type of shared state.

I've also added a new struct SharedStates which is used to manage the type-erased shared states. The SystemParam::init_state function accepts a reference to the shared states and implementers can use it to get typed pointers which it can store as part of the state. SystemParam::init_state is now unsafe to call. It is required to make sure the newly constructed state does not outlive the SharedStates.

Each type that is to be used as shared state must implement SystemParamSharedState which is used internally to make the vtables.

Open questions

How should DynSystemParam work?

Currently SystemParam::shared is an associated function so DynSystemParam can't forward to the concrete type. For now, I've added "Don't use shared state" to the safety requirements of DynSystemParam::new. Another possibility is to make it not share with anything else, but still share internally and log a warning.

Todo

  • redo impl of SystemParam for Commands to share their InternaCommandQueues
  • implement SystemParamSharedState in the SystemParam derive macro
  • include the pointers to shared states in derived implementation of SystemParam

Testing

I added a test case that confirms the shared state is actually shared.

Showcase

See the test case for basic usage. It shares an Arc<AtomicBool>. https://github.com/Person-93/bevy/blob/4cb020a0c9065f5bf363316677d49d60b1de4d51/crates/bevy_ecs/src/system/system_param.rs#L3529-L3602

@Person-93 Person-93 marked this pull request as draft February 11, 2026 06:34
@Person-93 Person-93 force-pushed the feature/system-param-shared-state branch 4 times, most recently from dfaca53 to b244844 Compare February 11, 2026 08:20
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide D-Complex Quite challenging from either a design or technical perspective. Ask for help! X-Contentious There are nontrivial implications that should be thought through S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Feb 11, 2026
@alice-i-cecile alice-i-cecile changed the title initial implmentation of shared state for system params initial implementation of shared state for system params Feb 11, 2026
@github-actions
Copy link
Copy Markdown
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Please review the instructions for writing migration guides, then expand or revise the content in the migration guides directory to reflect your changes.

@cart cart added this to ECS Feb 12, 2026
@github-project-automation github-project-automation bot moved this to Needs SME Triage in ECS Feb 12, 2026
@Person-93 Person-93 force-pushed the feature/system-param-shared-state branch from 08b3293 to 7d54953 Compare February 12, 2026 07:28
Thanks to Giooschi in discord for pinpointing the bug
@alice-i-cecile alice-i-cecile added the S-Needs-Goal This should have a C-Goal and should not continue until it has one label Feb 12, 2026
@alice-i-cecile alice-i-cecile moved this from Needs SME Triage to SME Triaged in ECS Feb 12, 2026
@Person-93
Copy link
Copy Markdown
Contributor Author

Putting an update here because this is still tagged as waiting on author.

I've stopped working on this because it needs a project goal and it doesn't seem likely to get one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Complex Quite challenging from either a design or technical perspective. Ask for help! M-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Goal This should have a C-Goal and should not continue until it has one S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Contentious There are nontrivial implications that should be thought through

Projects

Status: SME Triaged

Development

Successfully merging this pull request may close these issues.

Shared State for System Param

3 participants