[Original tutorial is based on UE 4.18, MINE is based on UE 4.25]

English original Address

The next tutorial will show you how to get the total number of minions in a scene. Create a new C++ Actor subclass and name it GetNumberOfPawns.

Nothing needs to be done in the header file, and here is the final header file.

GetNumberOfPawns.h

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "GetNumberOfPawns.generated.h"

UCLASS(a)class UNREALCPP_API AGetNumberOfPawns : public AActor
{
	GENERATED_BODY(a)public:	
	// Sets default values for this actor's properties
	AGetNumberOfPawns(a);protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay(a) override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

};
Copy the code

Next, add GetWorld()->GetNumPawns() to our BeginPlay() function. All actors have access to the GetWorld() function. You can see all the functions of UWorld here.

Here is our final.cpp file.

#include "GetNumberOfPawns.h"


// Sets default values
AGetNumberOfPawns::AGetNumberOfPawns()
{
 	// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AGetNumberOfPawns::BeginPlay(a)
{
	Super::BeginPlay(a); int32 MyPawns =GetWorld() - >GetNumPawns(a); GEngine->AddOnScreenDebugMessage(- 1.5.f, FColor::Green, FString::Printf(TEXT("Number of Pawns: %d"), MyPawns));
	
}

// Called every frame
void AGetNumberOfPawns::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}
Copy the code

Operation effect is shown in figure