Get Component by Class not returning components in hierarchical order

I am creating a BP component to add to actors in my level. When an actor with the component is selected, the component has a function that looks at the actors components for static meshes to do various things (change material, set mesh, etc.)

The root of each actor is a static mesh component, and many have sub-components that are static meshes as well. My issue is that when I select some actors, the resulting function returns a sub-component mesh rather than my root mesh component.

Screenshot 2022-06-14 143529

My question is: How does GetComponentByClass identify the order in which components are returned? It does not appear to be hierarchical like I hoped.

I know I can use GetComponentsByTag, but this seems like it will be more expensive and I am developing for a mobile platform.

Thank you!

It doesn’t understand hierarchy. If the root component is what you’re after, you could call GetRootComponent and then cast it to a static mesh component.

1 Like

Thanks Ghost this didn’t occur to me. Do you think it would be faster to do the cast or the for each loop from GetComponentsByTag?

I would imagine the cast would be in the noise for your situation while also producing more readable code/logic. Like always, performance testing is key to a question like that. Are you running this on tick or is this a oneshot?

I agree - the cast is cleaner and allows me to use the component in other projects without adding tags to each actor. The function is a oneshot.

Oneshot, then most definitely a cast.

Thanks again for your help!!