add example of creating systems via closures#2231
add example of creating systems via closures#2231cab wants to merge 1 commit intobevyengine:mainfrom
Conversation
|
could you also add the example to https://github.com/bevyengine/bevy/blob/main/examples/README.md? |
|
To pass parameters to your systems, you can use a Box::new(|mut cmd: Commands, arg: Local<String>| {
info!("this system uses the moved arg: {:?}", arg);
let id = cmd.spawn().id();
info!("also it spawned an entity {:?}", id);
})
.system()
.config(|config| config.1 = Some("hello".to_string())) |
|
Thanks for the review!
Interesting, I didn't know about this. Do you think this PR is still useful as an example? (cc @alice-i-cecile) |
Yes! We don't show how to pass closures as system, so that's valuable. And it would be even more with the |
| info!("this system uses an argument: {:?}", arg); | ||
| let id = cmd.spawn().id(); | ||
| info!("also it spawned an entity {:?}", id); | ||
| }).system().config(|config| config.1 = Some("hello".to_string()))) |
There was a problem hiding this comment.
| }).system().config(|config| config.1 = Some("hello".to_string()))) | |
| }).system()) |
Unless the .config() is completely necessary here, I feel like it's just adding unnecessary noise.
There was a problem hiding this comment.
I think it is necessary -- that's what sets the Local<String>.
|
The content itself looks solid, it but could use a bit more motivating text at the start, explaining why you might want to do this. |
Just to make sure I'm understanding -- should I add a comment/text to the example, or do you mean add more explanation to the PR description? |
|
The former please; it's much more important that end users can understand the motivation. And the reviewers can check the comments in the code ;) |
|
sorry for dropping this! will try to wrap it up by the end of this week. |
|
as seen on Discord, |
|
#2422 should help with that. |
|
Closing due to no response on relicensing. |
|
Closing to let someone else pick this work up :) Please credit the author of this PR if you build off it. |
Prompted by this discord discussion.
This example shows how to pass an arbitrary argument to a closure, and how to then use that closure as a
bevy_ecs::system::System.