I have a blueprint that has a number of components of the same type attached to it. I would like to be able to iterate through them, is this possible? At the moment, I am manually dragging a node on for each one and combining into an array. Is there a more robust way to do this?
Thanks.
there’s definitely a way to do that in C++, probably with the degree of control that you’re after. I’ve never used it, but have you looked at GetActorListFromComponentList node? It takes an array of components and outputs an array of actors, and it has a class filter. You could maybe filter for static mesh or whatever it is that you want to isolate and then cast from actor class on the other side of the array that it makes?
Yeah I saw that node, seems to do literally the inverse of what I need! Or have I misunderstood? If I need an array of components, surely no way of casting from an array of actors since components don’t inherit from actors?
Unless there is a way to do that, I shall write a C++ function to provide the node, thanks
Urr, yeah, that is correct. Sorry hadnt really thought it through. In C++ its pretty straightforwards.
TArray Components;
GetComponents(Components);
Then just check what component is what with IsA;
if (PrimComp->IsA(UStaticMeshComponent::StaticClass())) { blar blar}
Yup, just written a method in AActor to expose it to blueprint, so hopefully that’s all sorted… I’ll post it once I’ve checked it works, thank you
As discussed in the comments, there is no function which achieves this for blueprint but it can be easily added to code. I wrote the following method to extend the Actor class to allow for this functionality. When I get a minute to tidy it up, I will make a pull request on Github, until such a time here is the function I am using:
TArray<UActorComponent*> AActor::GetComponentsOfType(UClass* ClassFilter) const
{
TArray<UActorComponent*> ValidComponents;
for (UActorComponent* Component : OwnedComponents)
{
if (Component && Component->IsA(ClassFilter))
{
ValidComponents.Add(Component);
}
}
return ValidComponents;
}
The main limitation this has is, in bluprint, you must loop through the returned array and cast it to the actual component type you need.
Like I said, when I get some time, I’ll investigate into whether it is possible to have generic blueprint functions so the function could actually return the correct type.
Update
I have created a pull request, you can see it [here][1]:
[1]: