Skip to content

How to use bevy_reflect to get component field values at runtime? #7886

Discussion options

You must be logged in to vote

Just an update for anyone else who was curious, I was able to solve this by using ReflectRef to convert my dyn Reflect object into the a trait object of the subtrait Struct, and access the fields that way. Turns out it was literally on the landing page for bevy_reflect docs. Anyways here's the snippet I used:

let reflected = reflect_component.reflect(world, selected_entity).unwrap();
let ReflectRef::Struct(reflected) = reflected.reflect_ref() else { unreachable!() };
for (i, field) in reflected.iter_fields().enumerate() {
    ui.label(format!("{:?} : {:?}", reflected.name_at(i).unwrap(), reflected.field_at(i).unwrap()));
}

Obviously there's some mutability changes to be made to actually e…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by DumplingEater
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants