Manipulate non-send resources with Commands
#12819
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.
Objective
Resources
usingCommands
, it is not possible to do the same for non-send resources (NonSend
).bevy_command_non_send
#12795 for full justification and additional discussion.bevy_command_non_send
#12795.Send
resources out ofWorld
#9122. I'd prefer that get merged instead of this one, but... uhhh...Solution
init_non_send_resource
,insert_non_send_resource
, andremove_non_send_resource
methods forCommands
.Commands
is accessible across threads,insert_non_send_resource
's implementation takes a closure that creates the non-send resource on the main thread, instead of straight-up taking the value. This can be a bit confusing, so please check my documentation closely to make sure I explained it well!remove_resource
in 13e0f17, which is why the diff looks weird. I recommend looking at 314391c for the actual new content.Changelog
Commands
.Migration Guide
This PR is not actually a breaking change, but I feel a migration guide is useful. Comment if you think I should remove it. :)
It is now possible to insert and remove non-send resources using
Commands
. If you have any systems that take&mut World
like this:You can replace it with the following, which may yield performance improvements:
This also applies to the methods
init_non_send_resource
andremove_non_send_resource
. Note thatCommands::insert_non_send_resource
takes a closure, notMyNonSend
directly. This closure's returned value will be inserted, after it is run on the main thread.Be warned that this may change the order of inserting your resource!
Commands
works by delaying work untilapply_deferred
is called. If you depend on a resource being inserted / removed within a schedule, usingCommands
will break your code.