I’m looking for an efficient way to locate actors which have specific components attached. For example, I’ve extended UActorComponent to create a “Pickup” component which can be attached to any actor. This indicates that the actor can be picked up, and defines a few related events.
I’d like my AI to be able to locate these items within the level, make their way to them and collect them. The brute force way would be to grab an array of all actors in the level (using GetAllActorsOfClass), and iterate through checking for the pickup component. This is what I’m doing at the moment.
Is there a better way? I’ve looked through the engine source and can’t see anything obvious.
With the introduction of blueprintable components in the latest 4.7 build it seems like this could become a useful operation.
Hello,
When you spawn the actor which has the component, set a reference (variable with your pick up bp as value) from the output node of “SpawnActor” with it. Then you use reference to find it.
If you have more than one, you can do the same with an array. With a loop, your AI will reach all from 1 to last in array (with a check on a bool “picked up” to know when start moving to next) To move to closest first, you’ll have to check locations / lengths (and pass may be a trouble sometimes ^^)
Edit : If you set component after spawn, set reference when you set component.
Add an actor array to your game state. When you initialize your component, add the owning actor to that array (add unique). When you uninitialize your component remove the owning actor from the array.
Quick sketch:
I don’t understand the interest of setting an array of variables references of one element instead of a variable reference (which is same except that it is not an array.)