Skip to content

Allow freezing of command execution or capturing commands #20395

@makspll

Description

@makspll

What problem does this solve or what need does it fill?

Bevy 0.16 introduced this change: https://bevy.org/learn/migration-guides/0-15-to-0-16/#flush-commands-after-every-mutation-in-worldentitymut,
which causes commands to be flushed at entity or component modifications.

bevy_mod_scripting uses commands as a way of allowing users to execute callbacks on their scripts, which can arbitrarily affect &mut World including spawn/despawn entities, or add/remove components.

One thing these commands can also do is potentially queue other callback commands, which are expected to run only once the first callback is completed (because callbacks necessitate removing some resources from the world)

What this change means for BMS is the potential of a callback to run early if a script adds an entity, causing errors.

Ideally It should be possible for my callback runner to "capture" or "freeze" any new commands from executing until it itself completes.

What solution would you like?

Either:

  • Command capture mechanism, i.e. Commands::set_queue, which lets us push commands to a different queue (perhaps in a function scope to avoid gutting commands generally), and then Commands::append that queue when ready
  • Or Commands::freeze which sets a flag that prevents commands from applying for the duration that freeze is set
    But the two above don't let me "ignore" the entity commands, so that they can be flushed immediately, but then defer other commands (without some sort of filterin)

Maybe the simplest solution would be not flushing commands other than those necessary for EntityRefMut to work, mine (perhaps naive) assumption is that commands only ever get applied at ApplyDeferred sync points.

What alternative(s) have you considered?

I could:

  • pull out those resources into Arcs, and lock on every access, which doesn't require changes in Bevy
  • Make the command "retry" itself if it knows the handler state is missing, which could have false positives

Additional context

Example reproduction repo from a user facing this problem: https://github.com/Peepo-Juice/bevy_mod_min_repro
With the problem being seen here:
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-FeatureA new feature, making something new possible

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions