How to get all instances of a component?

I need to get all instances of a component so I can for loop through them so get instance count won’t work. Is there some sort of way to get all instances of a component?

Cast to bp > get component > get all component instances > for each loop > multigate > do thing to specific instance.

Problem is it doesn’t seem actually possible to do that I can get an instance count use that as a max random gen a float and interact with a specific instance that way but it works exactly one time I need a loop through each instance. And the complete off the for loop so I can actually do things when that is done.

Any help appreciated.

it depends on where is components. if in actor - get all actors of class (actor) - for loop - GetComponentsByClass (needen component) - if array length > 0 do thing.
better way register component in begin play - add to array or set var in actor (or where it is)

1 Like

You can’t do

Cast to bp > get component > get all component instances > for each loop > multigate > do thing to specific

You have to do:

Get all actors of class > for each > cast to BP > get compoent > do something

1 Like

I’m trying to replace random trees with dead ones when an event fires. For now I just want it to do it to every one then later I’ll have it do a multigate or something.

Problem is my for loop will not iterate through every instance.

Code

I’m trying to replace random trees with dead ones when an event fires. For now I just want it to do it to every one then later I’ll have it do a multigate or something.

Problem is my for loop will not iterate through every instance.

Code

This works for me, can you do something similar?..

I need instances of the component not all the components. Like there hism component and it has 70 instances.

Oops, sorry :-/

If you’re not using anything else that enables different design patterns like ECS or Component Systems, then one way to accomplish what you’re wanting would be to make a custom component to inherit from using a virtual “getSelfTypeArray” that you’d cast the return type of, and in every child component type you’d have this return a TArray (or address to it). This TArray would carry pointers to every instance of this type. Personally i find this sad and sloppy but at least it can scale okay in theory. Would be possible to make this cleaner perhaps if templating worked in BP. If you’re going for a data driven design i find that using a custom json object in every actor works okay. Maybe inside that you can have custom ways of retrieving all json objects (and their owning Actors) with a specific json member type. Or you can try shiving your own ECS pattern which am currently contemplating however not sure that’d be ideal.