3 ms·
From the getting started tutorial,[1] I notice that (certain) functions appear to have the method "system" added to them. For example: > Note the hello_world.s
by aazaa 6y ago
From the getting started tutorial,[1] I notice that (certain) functions appear to have the method "system" added to them. For example:
> Note the hello_world.system() function call. This is a "trait extension method" that converts the hello_world function into the System type. ...
Later follows an example where a function previously defined as add_people appears to have a method called "system", allowing expressions like this:
add_people.system()
I don't see any macros on the function definitions, so where does this come from. Also, what's the purpose of adding the system method to functions?
[1] https://bevyengine.org/learn/book/getting-started/ecs/ https://bevyengine.org/learn/book/getting-started/ecs/
- _cart 6y agoThe .system() method converts the function pointer to an actual System trait implementation. This is accomplished by implementing traits for functions that match a specific signature. Ex: Fn(SystemParam), Fn(SystemParam, SystemParam), etc. The .system() method uses the SystemParam type information to determine what resources are being accessed, which gets converted to logic that can pass the resource data in. It also uses the extracted type information to inform the system executor how to safely run each system in parallel.
- ratww 6y agothe system() method is part of the IntoSystem trait that converts the function into a System object: https://github.com/bevyengine/bevy/blob/master/crates/bevy_ecs/src/system/into_system.rs#L193 https://github.com/bevyengine/bevy/blob/master/crates/bevy_e... There are macros below that implement that for Func types.
- vbrandl 6y agoYou can define a trait and add an blanket implementation of that trait for `Fn()` Somewhat like this: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4c640afa36b18d956420dbce69a30de2 https://play.rust-lang.org/?version=stable&mode=debug&editio... Edit: updated playground link