Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Bevy "scope" its systems based on the type of the arguments?

Tags:

rust

bevy

Bevy, a new Rust game engine and ECS, has a feature where it "scopes" its systems based on the type of the arguments. From its docs:

The parameters we pass in to a "system function" define what entities the system runs on. In this case, greet_people will run on all entities with the Person and Name component.

It looks like this:

struct Person;
struct Name(String);

fn greet_people(person: &Person, name: &Name) {
    println!("hello {}", name.0);
}

How is Bevy able to make this happen? I thought I read somewhere that Rust didn't support reflection in this way.

like image 345
Rodney Folz Avatar asked Aug 20 '20 01:08

Rodney Folz


1 Answers

Bevy defines a set of traits (IntoQuerySystem and IntoForEachSystem) that are implemented by functions that match those signatures. Those traits are then exported by the Bevy prelude. One of the limitations of this is that you can only convert functions up to a certain number of arguments into systems, and the arguments must be in a particular order ([command?], [resources...], [queries/components...]).

Edit: For-each systems were deprecated in Bevy 0.4.

like image 168
TehPers Avatar answered Nov 12 '22 09:11

TehPers