#I'm a beginner of rust

6 messages · Page 1 of 1 (latest)

uncut flume
#
fn greet_people(query: Query<&Name, With<Person>>) {
    for name in &query {
        println!("hello {}!", name.0);
    }
}

I was wandering what query does, can someone explain? Or does someone have documentation about query?
is query a list? (sorry I was used to program simple code in c#)
why do we use & in front of Name and query, will this mean that we are saying that these variables will be modified, what does mean the "name.0" i mean what does stand .0
for print every name in the print function we are saying that in the {} each print function we will have the next name in the list?
I mean someone please explain me

grave berry
#

I would suggest getting familiar with Rust's basics before diving into Bevy, or you might get overwhelmed. Anyway, to answer your questions:

  • query is like an handle to all the entities that have a Name and a Person component, and which allows you to read the Name component. This is not quite a list, but you can think of it like one (except you can't index, pop, insert and stuff like that, only iterate).

  • the & in front of the query is there because it creates a reference to it, and references to Querys implement the IntoIterator trait which allows you to use them with for. This works the same as the stdlib's Vec type. The & doesn't allow you to mutate through it, like how it works with normal references.

#
  • the .0 is a way to access the first field of structs without named fields (i.e. struct Name(String); has one field of type String without name)
#

I'm not sure there's a field like that in bevy's Name struct though

#
  • for the last question, yes, the {} in println means that it will be replaced by whatever you are printing (in this case name.0)
uncut flume
#

ok thanks