#cuicui_layout
35 messages ยท Page 1 of 1 (latest)
๐ 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?
what does "dsl containing a Resource" means?
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
Sorry I still don't understand. what's "image of the resource character"?
Like a Handle<Image>?
Sorry, I'm still new to this. It inserts a ImageBundle, with a HighlightedCharacter bundle. I'd like the contents of ImageBundle to change as I change character icons.
Similar to Apex here https://www.gameuidatabase.com/index.php?&scrn=41
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>.
Give me a few minutes to grok that. I think it makes sense though
2 more questions sorry (also I couldn't grok the last one either, but I'm going to come back to it later):
- Firstly, I have a snippet like this and the despawn_screen func here (https://github.com/bevyengine/bevy/blob/main/examples/games/game_menu.rs#L816-L821) I'm curious how I'd despawn the background image
bg?
dsl! {&mut commands,
spawn(layout ">dCaC", screen_root, "root", image &bg) {...}
}
- Secondly, is this you? https://github.com/nicopap (
@Gibonus if that wasn't clear)
yeah that's me
The background image is a component of UI nodes. image adds the UiImage component to the entity of the given DSL statement (each statement spawns an entity)
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.
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 withspawnspecifically
What other things did you add?
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
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
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
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) {...}
}
FYI, cmds.spawn(โฆ) is how bevy's spawning system works by default!
Consider using it over the dsl! macro if you are more comfortable with it
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
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:
- the "This will expand into" code listing here: https://docs.rs/cuicui_dsl/latest/cuicui_dsl/macro.dsl.html#spawn
- the "source" link in the
impl IntoEntityCommands for &'a mut Commandsin the "implementation on foreign types" section https://docs.rs/cuicui_dsl/latest/cuicui_dsl/trait.IntoEntityCommands.html
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))
Ah, this is all becoming a lot clearer. Thank you so much for the help!