-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Add register_system_cached to DeferredWorld. #21972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add register_system_cached to DeferredWorld. #21972
Conversation
| /// If you want to access values from the environment within a system, consider passing them in | ||
| /// as inputs via [`Commands::run_system_cached_with`]. If that's not an option, consider | ||
| /// [`Commands::register_system`] instead. | ||
| pub fn register_system_cached<I, O, M, S>(&mut self, system: S) -> SystemId<I, O> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe get_or_insert_system would be more idiomatic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
World already has register_system_cached, and so I named this function likewise.
|
Can you add the test you made for it to the test suite? |
I'll try! |
|
I think I have added the test successfully, and it passes. |
|
This pattern of sometimes, but not always, deferring work with a command can make code hard to reason about. I think that's what you describe above as a "minor flaw", but I worry it's actually more insidious. Like, if you use this in an We've been bitten by similar issues with sometimes-deferred work and Is the goal here to have a type-erased version of Would it work to use a function pointer? If you do something like let f: fn(&mut World) -> Result<(), RegisteredSystemError> =
|world| world.run_system_cached(some_system);then you have something |
Objective
Allow registering systems when uncached, while inside
on_addand its ilk.Solution
Add
register_system_cachedtoDeferredWorld.Testing
I tried it in an
on_addand it seemed to work just fine. The first time the component was spawned, it took the uncached branch and registered the system. The second time the component was spawned, it took the cached branch and retrieved the cachedSystemId.Minor Flaw
If this function is called twice for the same system before commands are executed, then it will register the system twice.
If you wish me to, then I can make it send out a warning when this occurs.