#Problem passing asset server to builder sub-functions

21 messages · Page 1 of 1 (latest)

little marlin
#

I have a bevy function that renders a dynamic table. I need to be able to spawn sprites in the various cells of the table, and therefore I need to be able to access Commands and asset-server. What is the best way to pass in commands and asset server into sub-functions? Passing in asset-server is compiling but causing a QueryDoesNotMatch(117v0) panic.... Is there a way to pass asset-server cleanly into the subfunction?

 builder
                .spawn(NodeBundle {
                    style: Style {
                        height: Val::Percent(100.0),
                        width: Val::Percent(100.0),
                        aspect_ratio: Some(1.0),
                        display: Display::Grid, 
                        padding: UiRect { left: Val::Px(0.0),right:Val::Px(0.0),top: Val::Px(1.0),bottom: Val::Px(1.0) },
                        grid_template_rows: vec![
                            GridTrack::percent(5.0),
                            RepeatedGridTrack::flex(10, 1.0),
                            GridTrack::percent(5.0),
                        ],
                        grid_template_columns: RepeatedGridTrack::flex(3, 1.0),

                        row_gap: Val::Px(0.0),
                        column_gap: Val::Px(0.0),
                        ..default()
                    },
                    background_color: BackgroundColor(Color::DARK_GRAY),
                    ..default()
                })
                .with_children(|builder| {

                    item_rect(builder, Color::ORANGE);
                    item_rect(builder, Color::BISQUE);
                    item_rect_double(builder, Color::WHITE);

                    for i in 0..10 {

                        item_rect(builder, Color::GRAY);
                        item_rect(builder, Color::GRAY);
                        item_rect_double(builder, Color::GRAY);
                    }

                    item_rect_bottom(builder, Color::GRAY,);


                
#
fn item_rect_bottom(
                   // commands: &mut Commands,        
                  //  asset_server: &Res<AssetServer>,
                    builder: &mut ChildBuilder,     
                    color: Color,                   
                    ) {
    builder
        .spawn(NodeBundle {
            style: Style {
                display: Display::Grid,         
                padding: UiRect { left: Val::Px(0.0),right:Val::Px(0.0),top: Val::Px(0.0),bottom: Val::Px(1.0) },
                row_gap: Val::Px(0.0),          
                column_gap: Val::Px(0.0),       
                grid_column: GridPlacement::span(3), 
                //max_height: Val::Percent(5.0),
                ..default()
            },
            background_color: BackgroundColor(Color::BLACK),
            ..default()
        })
        .with_children(|builder| {      
            builder.spawn(NodeBundle {      
                background_color: BackgroundColor(color),
                ..default()
            });
#

But what I want is to spawn a spritebundle inside the item_rect_bottom function. I need commands and assetserver

#

The following does not work

#
fn item_rect_bottom(
                  //  commands: &mut Commands,    
                  
                   asset_server: Res<AssetServer>,
                    builder: &mut ChildBuilder,     
                    color: Color,                   
                    ) {
    builder
        .spawn(NodeBundle {
            style: Style {
                display: Display::Grid,         
                padding: UiRect { left: Val::Px(0.0),right:Val::Px(0.0),top: Val::Px(1.0),bottom: Val::Px(1.0) },
                row_gap: Val::Px(0.0),          
                column_gap: Val::Px(0.0),       
                grid_column: GridPlacement::span(3), 
                //max_height: Val::Percent(5.0),
                ..default()
            },
            background_color: BackgroundColor(Color::BLACK),
            ..default()
        })
        .with_children(|builder| {      
    //        builder.spawn(NodeBundle {
    //            background_color: BackgroundColor(color),
    //            ..default()
    //        });
                

                builder
                    .spawn(SpriteBundle {
                        texture: asset_server.load("gradient.png"),
                        sprite: Sprite {custom_size: Some(Vec2 {x: 250.0, y: 120.0}),
                                        ..Default::default()},
                        transform: Transform {
                            translation: Vec3::new(0.0,0.0,100.0),
                            rotation: Quat::from_xyzw(0.0,0.0,0.0,1.0), 
                            ..Default::default()},
                            ..default()
                    });
            .insert(TableBottomSprite);
        });
}
small sandal
#

What exactly doesn't work? Do you get a compile error?

little marlin
#

Seems to be tied to the asset_server

#

I get a compile error if I try to pass commands in

#

but I think builder.spawn maybe works without passing commands in. so I just cant get the asset_server

#

The panic has QueryDoesNotMatch(117v0) as an error

small sandal
#

That error doesn't seem to be related at first glance 🤔 This is a panic you get when you call myquery.get(myentity) when myentity doesn't exist

little marlin
#

So if I comment out asset server and then replace the spawn.spritebundle with a nodebundle, it works fine.

#

Adding in the sprite bundle causes the entity not found error. I assumed it was related to asset_server, but i guess maybe its not as you say. So for starters, it IS possible to spawn spritebundles in the builder?

#

are there any examples of this floating around that you are aware of?

small sandal
little marlin
#

ahhhh

#

oh well.. I was hoping to do some animation in the cells.. .I guess that's out.

#

thanks though.. At least I have my answer

little marlin