Array of actors that inherit interface?

Hello. I’m trying to add a variable for my blueprint, which is an array that holds all actors that inherit the IPerson interface. The use-case is for a building that wants to keep track of who is inside the building. I would like the array to be generic, in that as long as the object implements IPerson, it can be inside the building. Is there any way to do this?

When you declare the array, you can select a type for it. Try typing the name of your interface. If its pops up chose Object ‘you interface name’.

…To be honest, I never tried creating an array of interface type. So this may not work.

If that does nor work, another thing to try is to define a Base bleuprint which implements ‘IPerson’ called PersonBase. Then extend from this BP to create the different Person classes. Later when it comes time to create the array, select Object’PersonBase_C’ as the type.

So the hierarchi will be:

IPerson ----> PersonBase → PersonA, PersonB…

In Java terms:

PersonBase extends Actor (or any other class) implements IPerson

PersonA extends PersonBase

PersonB extends PersonBase

As far as I know, the items from the array will be of the declared type of the array.

So suppose I declare the array as of type Object’PersonBase_C’, then when you get an item back from the array, it will be of that type. However if you want to get the actual type (suppose you added PersonA or PersonB which are extened from PersonBase), you will need to cast them to their correct type.

But in normal cases, that cast is not necessary. If you find yourselves in need of such a cast, you need to rethink your class hierarchy. You should define all the required functions that the Persons should expose in the base class itself. Then you can override them in the extended classes. The overriden version will be called even if they are not cast to the real type.

Yeah, i decided to go with a base class, and have the blueprints extend it. Will the array still hold the extended classes, tho? or will i have to cast them to the base class before i add them to the array? My C++ polymorphism is a little bit rusty