[C++] How do I access a class from an array of actors returned from GetAllActorsOfClass?

Hello, I’m new to UE4 and C++. I have a C++ class called AFoodDistribution, and create BP class based on it. Now I put multiple BP into the scene, and hope to store them in an array, so I used UGameplayStatics::GetAllActorsOfClass(GetWorld(), AFoodDistribution::StaticClass(), foundFoods);
to store it in TArray<AActors*> foundFoods;

Now I hope to access a function called AFoodDistribution::changeHealth() under the class AFoodDistribution from another class, let’s call it AFish. I hope the fish can detect where the foodDistribution is, and get health from the distribution when nearby. But when I tried to use foundFoods[I]->changeHealth(), it shows error that changeHealthAmount': is not a member of 'AActor'

How can I fix it? Thank you sooooooo much in advance!!

1 Like

You will have to cast each element to AFoodDistribution before you can access its members.

I recommend you use a TActorIterator instead.

1 Like