# Counting blueprint class instances from C++

**URL:** <https://forums.unrealengine.com/t/counting-blueprint-class-instances-from-c/1255031>\
**Category:** C++\
**Tags:** type, unreal-engine, Blueprint, question, CPP\
**Created:** [August 8, 2023, 10:11pm UTC](https://forums.unrealengine.com/t/counting-blueprint-class-instances-from-c/1255031 "2023-08-08T22:11:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![NightmareGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nightmaregames/32/711529_2.png) [@NightmareGames](https://forums.unrealengine.com/u/NightmareGames)\
**Post date:** [August 8, 2023, 10:11pm UTC](https://forums.unrealengine.com/t/counting-blueprint-class-instances-from-c/1255031/1 "2023-08-08T22:11:44Z")

</div>

I’d like to get a count of the number of enemies left in the level to display in the UI.

The way I’m trying to do that is with a call to UGameplayStatics::GetAllActorsOfClass() from inside a custom GameState class.

However, the actors I need to count are blueprints. It’s unclear to me how to make that blueprint class a recognizable type in C++ in order to pass it to this function. It’s looking for `TSubclassOf<AActor> ActorClass`, which implies the class needs to be a C++ class inheriting from AActor. Is that even posssible?

Must I rewrite my entire enemy class in C++ to get this working?  
Would it be easier to just do the operation in blueprints and send the resulting integer to the UI widget, bypassing C++ entirely?  
Is there a third option I haven’t considered?

---

<div class="post-metadata">

**Author:** ![egr](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/egr/32/249903_2.png) [@egr](https://forums.unrealengine.com/u/egr)\
**Post date:** [August 8, 2023, 11:14pm UTC](https://forums.unrealengine.com/t/counting-blueprint-class-instances-from-c/1255031/2 "2023-08-08T23:14:46Z")

</div>

You could just add an “enemy” tag to your enemy actors blueprints and use GetAllActorsWithTag instead, that way the child class is irrelevant, since it seems you just want to count them.

---

<div class="post-metadata">

**Author:** ![egr](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/egr/32/249903_2.png) [@egr](https://forums.unrealengine.com/u/egr)\
**Post date:** [August 8, 2023, 11:36pm UTC](https://forums.unrealengine.com/t/counting-blueprint-class-instances-from-c/1255031/3 "2023-08-08T23:36:06Z")

</div>

Or something like this seems to work:

```auto
	TArray<AActor*> Actors;
	auto Path = FString("/Game/Experimental/PCG/NewBlueprint");
	const auto BpClass = ConstructorHelpersInternal::FindOrLoadClass(Path, AActor::StaticClass());
	UGameplayStatics::GetAllActorsOfClass(this, BpClass, Actors);
	UE_LOG(LogTemp, Warning, TEXT("Actor count %d"), Actors.Num());

```

---

<div class="post-metadata">

**Author:** ![Roy\_Wierer.Seda145](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/roy_wierer.seda145/32/906551_2.png) [@Roy\_Wierer.Seda145](https://forums.unrealengine.com/u/Roy_Wierer.Seda145)\
**Post date:** [August 9, 2023, 12:13am UTC](https://forums.unrealengine.com/t/counting-blueprint-class-instances-from-c/1255031/4 "2023-08-09T00:13:53Z")

</div>

> [@NightmareGames](#):
>
> `TSubclassOf<AActor> ActorClass`, which implies the class needs to be a C++ class inheriting from AActor

This means any class inheriting from AActor. Both blueprint and c++ classes can be this. A playercontroller, pawn, character, gamemode, and so on are all deriving from AActor.

Meaning, when you call `UGameplayStatics::GetAllActorsOfClass` using “Actor” as a class to search for, it will find spawned objects of all the above classes both c++ and blueprints. This function is used while playing the game and retrieves instances of classes, meaning it doesn’t count blueprint files but instances present in the world.

The `TSubclassOf<AActor>` type means “A class of AActor or any class deriving from AActor”.

> 

More on deriving classes (class inheritance) in OOP:

> [Inheritance (object-oriented programming) - Wikipedia](https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming))

---

<div class="post-metadata">

**Author:** ![NightmareGames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/nightmaregames/32/711529_2.png) [@NightmareGames](https://forums.unrealengine.com/u/NightmareGames)\
**Post date:** [August 10, 2023, 5:12am UTC](https://forums.unrealengine.com/t/counting-blueprint-class-instances-from-c/1255031/5 "2023-08-10T05:12:21Z")

</div>

For reasons unexplained, `GetAllActorsOfClass` didn’t work with either technique. It compiled and ran, but the function always returned an empty set.

Adding the tag and using `GetAllActorsWithTag` was easy, quick, and seems to work beautifully.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [September 9, 2023, 5:12am UTC](https://forums.unrealengine.com/t/counting-blueprint-class-instances-from-c/1255031/6 "2023-09-09T05:12:43Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
