#Trying to spawn multiple elements

28 messages · Page 1 of 1 (latest)

balmy moat
#

I want to itinerate in something like that :

#[derive(Component)]
pub struct CardTitle;

let ui_elements = vec![
            (Box::new(CardTitle), JustifyContent::Center, 215.0, 10.0, 50.0),
            (Box::new(CardDescription), JustifyContent::Start, 880.0, 50.0, 40.0),
        ];

I found this issue https://github.com/bevyengine/bevy/issues/3227 and sends me to insert_batch but I don't realy understand how to use that and can't find an example.

Maybe I'm missing how to make this work in a different way ?

GitHub

What problem does this solve or what need does it fill? Working with dynamic collections of components (as might be used in advanced spawning patterns) is very painful at the moment: every straight...

onyx thorn
#

What do you mean? Spawning multiple elements in different bundles at the same time, or spawn them in a bundle?

balmy moat
# onyx thorn What do you mean? Spawning multiple elements in different bundles at the same ti...

something like that

let ui_elements = vec![
            // (Box::new(CardTitle), JustifyContent::Center, 215.0, 10.0, 50.0),
            (Box::new(CardDescription), JustifyContent::Start, 880.0, 50.0, 40.0),
        ];

        for (component, justify_content, top, left, font_size) in ui_elements {
            commands
            .spawn((
                Node {
                // Cover the whole image
                width: Val::Percent(100.),
                height: Val::Percent(100.),
                flex_direction: FlexDirection::Column,
                justify_content: justify_content,
                align_items: AlignItems::Start,
                padding: UiRect::all(Val::Px(left)),
                top: Val::Px(top),
                ..default()
                },
                TargetCamera(texture_camera),
            ))
            .with_children(|parent| {
                parent.spawn((
                Text::new(""),
                TextFont {
                    font_size: font_size,
                    ..default()
                },
                TextColor(Color::srgb(31.0 / 255.0, 34.0 / 255.0, 37.0 / 255.0)),
                *component,
                CardText {
                    text: "".to_string(),
                    new_text: "".to_string(),
                },
                ));
            });
        }
wild tinsel
#

What's wrong with just doing a loop?

#

there is a method called spawn_batch that does something like this, but I don't think it will let you spawn things with children as the spawn command does.

onyx thorn
#

Yeah why not a loop? I've used it to do similar things like this and it works fine

wild tinsel
#

spawn batch or insert batch are good options if you are doing this with thousands of entities at a time and it becomes a performance issue

onyx thorn
#

I don't think this came from the idea of optimizing it, but sure

balmy moat
#

@wild tinsel @onyx thorn the issue is that my example does not work since I can't use different components in the vec! I get a "mismatched types" error

onyx thorn
#

Oh

balmy moat
#
error[E0308]: mismatched types
   --> src/entity/card/mod.rs:200:23
    |
200 |             (Box::new(CardDescription), JustifyContent::Start, 880.0, 50.0, 40.0),
    |              -------- ^^^^^^^^^^^^^^^ expected `CardTitle`, found `CardDescription`
    |              |
    |              arguments to this function are incorrect
    |```
onyx thorn
#

Can't you use a () instead of a vector then

#

A tuple yeah that's the name

balmy moat
onyx thorn
#

Yeah I... did not think a lot about what I said, sorry

balmy moat
onyx thorn
#

The docs have some examples

#

I mean the best thing I can think of to fix this is

#

To use a single component

#

And add a public variable to it that says if it's the name or description, etc

balmy moat
onyx thorn
#

@balmy moat

#

You should test if you can use dyn first

#

Like, defining the vector type manually

let ui_elements: Vec<(Box<dyn Component>, JustifyContent, f32, f32, f32)> = ...
wild tinsel
#

There are some ways to have type erased components, using reflection. But it's not super ergonomic in my opinion.

#

Depending on how complicated your use case is, it might be better to create an enum for your various cases and to write some code to spawn bundles depending on that

balmy moat
#

yeah, I'll find a way but I wanted to know if there was a different solution to the problem, while this issue is not solved.