C++ Interface Iterator

Hi Community,

I need to iterate over objects that Implement certain Interface.

I have seen that there’s a solution in blueprints:

I’d be great having something like:

for (TInterfaceIterator<ISavable> SaveActorItr(World); SaveActorItr; ++SaveActorItr)
{
	FSaveLoadInitClassData InitCD = SaveActorItr->GetSaveLoadInitClassData();
	ToBinary << InitCD;
	ToBinary << **SaveActorItr;
}

I have seen this solution, from the awesome Rama, but It seems to me that there must be a rather elegant way of doing it.

IToStringInterface* TheInterface = NULL;
for ( TObjectIterator<AActor> It; It; ++It )
{
	//Try InterFaceCasting
	TheInterface = InterfaceCast<IToStringInterface>(*It);
 
	//Run the Event specific to the actor, if the actor has the interface
	if(TheInterface) ClientMessage(TheInterface->ToString());
}

Any ideas?

Thanks very very much.

Cheers,

Bullyproof

You can use this blueprint function in C++ too, and so, use UGameplayStatics::GetAllActorsWithInterface to get all the actors using a specific interface.
But you still have to cast your interface or use IISavable::Execute_GetSaveLoadInitClassData(actor). But I am not sure if you can get a result with the auto-generated Execute_* functions (didn’t try).

I was going to publish the solution that I found here Finding all actors which inherit an interface, and call a corresponding interface function in C++? - C++ - Unreal Engine Forums.

It does seem to work.

Begounet, I forgot to say thanks!

Thanks!