Best way to expose a getter/setter for a component of a child of an entity? #18045
Unanswered
elenakrittik
asked this question in
Q&A
Replies: 1 comment
-
I suppose there are multiple solution. Here is a solution I often use (something like) : #[derive(Component)]
pub struct TextInput {
pub content: String,
}
#[derive(Component)]
struct TextInputTag;
fn create_text_input(
trigger: Trigger<OnAdd, TextInput>,
mut commands: Commands, inputs: Query<&TextInput>
) {
let input = inputs.get(trigger.entity()).unwrap();
commands.entity(trigger.entity()).with_children(|parent| {
parent.spawn(...);
parent.spawn((Text(input.content.clone()), TextInputTag));
parent.spawn(...);
});
}
fn update_text(
inputs: Query<&TextInput, Changed<TextInput>>,
mut texts: Query<(&mut Text, &Parent), With<TextInputTag>>
) {
for (mut text, parent) in &mut texts {
if let Ok(input) = inputs.get(**parent) {
text.0 = input.content.clone();
}
}
}
pub struct InputTextPlugin;
impl Plugin for InputTextPlugin {
fn build(app: &mut App) {
app.add_system(Update, update_text).observe(create_text_input);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Sorry for a cumbersome title.
I have a
TextInput
component that's designed to be used as a UI entity/node. To display the text i use bevy's builtinText
UI node and for technical reasons it needs to be nested in a few parent/child layers belowTextInput
instead of it andTextInput
existing side-by-side on the same entity. A visual may be easier to understand:What would be the best way to expose a getter/setter for
Text.0
(the textString
) in a way that doesn't require the user of aTextInput
to query and/or know about any of the "intermediate", private nodes?Beta Was this translation helpful? Give feedback.
All reactions