#How to pass system parameters (Query, Res<>, etc) with normal parameters?

10 messages · Page 1 of 1 (latest)

karmic crest
#

Hello I'm new in Bevy and Rust, therefore please forgive me if this is a dumb question.

Context here.
I'm currently making a generic button function, which can pass custom on-click logic. Here's the function:

create_button("tile_enter": &str, "Enter": &str, 25.0: f32, 
  ||{
    let str: String = read_input_field("tile_width": &str, world: &mut World);
    println!("{}", str);
  }: fn(),
  parent: &mut ChildBuilder
);

This button needs to read the value of my input field on click, and then do something afterward. The problem is within the on-click function.

Because I don't know if there's a way to pass system parameters with normal parameters (eg. i8, &str, etc), I need to pass the world: &mut World from the root function into read_input_field(&str, &mut World).

I need to use the world: &mut World for querying value inside input field. Here's the function:

pub fn read_input_field(id: &str, world: &mut World) -> String {
    let mut text_query = world.query::<(&InputFieldInner)>();

    for input_field_inner in text_query.iter(&world) {
        if input_field_inner.id.ne(id) {
            continue;
        }
        return input_field_inner.text.to_string();
    }
    return String::new();
}

Firstly, I could not find a way to pass in the world: &mut World as parameter.

Secondly, I don't know if it's a good way to query components using world.query.

Thirdly, I don't know whether I could pass in system parameters with normal parameters within the same function (eg. fn example(query: Query<&Text>, random_data: &str))

It would be appreciable if someone could educate me regarding this topic. ❤️

lethal girder
#
  1. Can't help with that one, never used world.
  2. From what I'm seeing, you can just give the first function a query: Query<&InputFieldInner> the use for item in &query{}
  3. Yes, you can
karmic crest
#

How for [3.] exactly?

lethal girder
#

(Could be that I got your question wrong though)

karmic crest
#

Thanks a lot. I switched to use egui instead of basic bevy ui. It's a lot easier...

#

I found out using Res<T> to store UI data instead of storing them inside the UI's components is a lot easier to handle.

cinder mortar
#
  1. &mut World is a valid SystemParam, but it has to be in the first position
  2. You should probably use a Query SystemParam directly
  3. You can pass around the &Query to other inner functions
karmic crest
cinder mortar
#

yes, that's correct. Also they prevent your systems to run in parallel, so they should be used only when you really need full World acces