Edit2: I posted a tutorial on how to do this, thanks to Marc’s help
Link: How to Iterate Over Only Specific Class of All Actors in World
Edit: Solution as Per Marc:
TActorIterator< AYourActorClass > CustomActorItr =
TActorIterator< AYourActorClass >(GetWorld());
~
Dear Friends at Epic,
This should be an easy question (but it is still unresolved)
I figured out how to use the regular Actor Iterator, and am currently filtering out class types manually.
I posted an example of All Actor iterator on the forum:
void AYourControllerClass::PrintAllActorsLocations()
{
//EngineUtils.h
FActorIterator AllActorsItr = FActorIterator(GetWorld());
//While not reached end (overloaded bool operator)
while (AllActorsItr)
{
ClientMessage(AllActorsItr->GetClass()->GetDesc());
ClientMessage(AllActorsItr->GetActorLocation().ToString());
//next actor
++AllActorsItr;
}
}
I would love to use the built in functions for filtering actors, but I just cannot figure out what template parameter type I am supposed to provide:
TActorIterator TheItr = TActorIterator(GetWorld);
I’ve tried variations like
TActorIterator TheItr = TActorIterator(GetWorld);
TActorIterator TheItr = TActorIterator(GetWorld);
and about 40 other things.
But I just cannot figure this out
Could you please show me an example of code for instancing a TActorIterator?
I know how to iterate over them and all that, I just cant instance it and supply template parameters that it accepts
EngineUtils.h
/**
* Template filter to select actors based on their type.
*/
template
class TActorTypeFilter :
public FActorFilter
{
public:
/**
* Determines if the actor should be returned during iteration or not
*
* @param ActorIndex ignored
* @param Actor Actor to check
* @return true if actor is selected, false otherwise
*/
FORCEINLINE bool IsSuitable( int32 /*ActorIndex*/, AActor* Actor ) const
{
return Actor ? Actor->IsA(ActorType::StaticClass()) : false;
}
};
/**
* Template actor iterator. Uses TActorTypeFilter to iterate over actors of the template type.
*/
template
class TActorIterator :
public TActorIteratorBase< TActorTypeFilter >
{
public:
/**
* Constructor, inits the starting position for iteration
*/
TActorIterator( UWorld* InWorld )
: TActorIteratorBase< TActorTypeFilter >( InWorld )
{
++(*this);
}
/**
* Returns the current suitable actor pointed at by the Iterator
*
* @return Current suitable actor
*/
FORCEINLINE ActorType* operator*()
{
check(FActorIteratorBase::CurrentActor);
checkf(!FActorIteratorBase::CurrentActor->HasAnyFlags(RF_Unreachable), TEXT("%s"), *FActorIteratorBase::CurrentActor->GetFullName());
return CastChecked(FActorIteratorBase::CurrentActor);
}
/**
* Returns the current suitable actor pointed at by the Iterator
*
* @return Current suitable actor
*/
FORCEINLINE AActor* operator->()
{
check(FActorIteratorBase::CurrentActor);
checkf(!FActorIteratorBase::CurrentActor->HasAnyFlags(RF_Unreachable), TEXT("%s"), *FActorIteratorBase::CurrentActor->GetFullName());
return CastChecked(FActorIteratorBase::CurrentActor);
}
};
Thanks!
Rama