#cuicui_layout

35 messages ยท Page 1 of 1 (latest)

arctic mural
rough crypt
#

๐Ÿ‘‹ in a OnEnter system, I setup a dsl containing a Resource. On an Update system I update that resource. How could I update the value of the resource being used in the dsl?

safe lotus
#

what does "dsl containing a Resource" means?

rough crypt
#
fn character_select_setup(
    mut commands: Commands,
    asset_server: Res<AssetServer>,
    character: Res<ChosenCharacter>,
) {...}

The dsl has a code snippet that grabs the image of the resource character, I'd like that image to update

safe lotus
#

Sorry I still don't understand. what's "image of the resource character"?

#

Like a Handle<Image>?

rough crypt
safe lotus
#

Do you have the Entity with the UiImage of the character at this point (when you want to update the image)?

#

In which case. I'd keep a resource with all the Handle<Image> of characters, then use a Query<&mut UiImage>, set the ui_image.texture with the new Handle<Image>.

rough crypt
#

Give me a few minutes to grok that. I think it makes sense though

rough crypt
safe lotus
#

yeah that's me

safe lotus
#

You would need to add a marker component to the Entity in question, then remove the entity's UiImage component.

#

There should be a better way of doing it, but it's not implemented in cuicui_layout yet.

rough crypt
#

You would need to add a marker component to the Entity in question, then remove the entity's UiImage component.
I think that's where I'm struggling. I've added other things in the dsl, and attached marker components to those, but wasn't sure how to do it with spawn specifically

safe lotus
#

What other things did you add?

rough crypt
#

For example, I've added a text_area(s.character().name, selection &s, text_style text_style), which is a custom function in my MenuDsl, and in that functions cmds.insert I added a component OnCharacterSelectPluginScreen, which is what I'm despawning. So I'm able to get rid of everything but the bg image from earlier

#

I guess I could add a marker function that takes in a component. Which would make the functions I've made more extensible and less prone to side effects? But still not sure on the bg

safe lotus
#

hmm this gives me an idea.

#

In the short term, I would add a bool field to MenuDsl and check it in the DslBundle::insert, add it to the spawned entity if it is checked.

#

Then add a method that sets the field to true.

#

An alternative that is definitively much more overhead is store a Vec<Box<dyn FnOnce(&mut EntityCommands)>> in your MenuDsl, and have a method that looks like:

fn add_bundle(&mut self, bundle: impl Bundle) {
  self.dyn_bundle_fns.push(Box::new(move |cmds| cmds.insert(bundle)));
}
``` Then in `insert`, you can drain the vec like so:
```rust
fn insert(&mut self, cmds: &mut EntityCommands) -> Entity {
  // .. do other things with cmds
  self.dyn_bundle_fns.drain(..).for_each(|f| f(cmds));
  // .. do other things with cmds
}
``` This _should_ work, but this should also be something you don't implement yourself.
#

But it's weird, and philosophically I still have to grasp what the place of such a method is in the universe.

#

Because what's the point of doing

dsl! { &mut cmds,
  spawn(add_bundle Marker1, add_bundle (SpatialBundle::default(), Foobar, Baz));
}
``` When instead you can do
```rust
cmds.spawn((Marker1, SpatialBundle::default(), Foobar, Baz));
#

hmm so many ideas

rough crypt
#

You have lost me once more, but I'm glad there's some ideas (the cmds.spawn(...) does look neat though) ๐Ÿ˜„
I'm a bit lost on the bool field suggestion. I get I can make my own insert function that calls the DslBundle::insert function, but I'm not sure how I'd pass in the OnCharacterSelectPluginScreen to attach to it in my case

safe lotus
#

Well for bool field

struct MenuDsl {
  on_character_select: bool, // false by default
}
impl MenuDsl {
  fn on_character_select(&mut self) {
    self.on_character_select = true;
  }
}
impl DslBundle for MenuDsl {
  fn insert(โ€ฆ) -> Entity {
    // ...
    if self.on_character_select {
      cmds.insert(OnCharacterSelectPluginScreen);
    }
    // ...
  }
}
#

The dsl would look as follow:

dsl! {&mut commands,
    spawn(layout ">dCaC", screen_root, "root", on_character_select, image &bg) {...}
}
safe lotus
#

Consider using it over the dsl! macro if you are more comfortable with it

rough crypt
#

Oh, is that what the spawn in the dsl! calls?

#

In VSCode I can't click into spawn like I can column, row, or even my own functions

safe lotus
#

spawn has special handling, since it needs to be called on the cmds and not the DslBundle, this is why it doesn't show up in VSCode. Specifically, that's what IntoEntityCommands::to_cmds calls.

Check this out:

So basically it's pretty much a wrapper around cmds.spawn (though, it does let entity_cmd = cmds.spawn_empty(); dsl_bundle.insert(entity_cmd))

rough crypt
#

Ah, this is all becoming a lot clearer. Thank you so much for the help!