Alternative way to use GetNumPawns()

Alternative way to use GetNumPawns()?

GetNumPawns() is deprecated since 4.23. What is the proper way to implement the same logic?

int32 UWorld::GetNumPawns() const
{
	int32 NumPawns = 0;
	for (APawn* Pawn : TActorRange<APawn>(const_cast<UWorld*>(this))) // For backwards compat GetNumPawns needs to remain const, but TActorRange can't use a const UWorld.
	{
		++NumPawns;
	}

	return NumPawns;
}

Answer for my question:

We can create 2 variants in a custom function:

#include "EngineUtils.h" // TActorRange, TActorIterator

int32 NumPawns = 0;
for (APawn* Pawn : TActorRange<APawn>(GetWorld())) 
{
	++NumPawns;
}

or

int32 NumPawns = 0;	
for (TActorIterator<APawn> ActorItr(GetWorld()); ActorItr; ++ActorItr )
{
	++NumPawns;
}
1 Like