Get all child classes of a blueprint class (not actors, but the classes)

I have a blueprint class (MotionBaseClass) with several child classes for different motions. Now I know that I can get all objects from a class, but is tehre a way that I can get an array of all classes that have a certain parent class? Basically I want to dynamically create an array at BeginPlay event to gather all child classes of MotionBaseClass.

So far I haven’t found a way to do this in Blueprint. Any ideas?

Make an array of MotionBaseClass.

Add every of your ChildClasses to it.

On even begin play loop through your MotionBaseClass array and “GetAllActorOfClass” in the each loop.

Add every actors the GetAllActor node found to your array of MotionBase Actors.

Thanks. I have this as my current workarround. But with this I have to manually set the array of child classes which becomes problematic when you have a lot of child classes since I would have to maintain this list. What I want to do is iterate over all child classes on begin play and add them to my array of Motions.

GetAllActor of MotionBaseClass should work for that purpose.

How? The classes are not instantiated at this point.

Oh yeah sorry you are right. I misunderstood.

I can’t think of any proper & quick way to do it indeed.

Try working around the keyword “subclass” with the context sensitivity to see what it gives you but that’s my last shot and it’s a poor one.

Custom Blueprint node for gettting an array of classes

Okay, I wrote a custom blueprint node that gets the job done. If anyone else is interested in this, please bear in mind that I basically have no clue when it comes to c++

So any suggestions/improvements to this are very welcome.

I created a c++ blueprint function library class…

.h



// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "CustomNodes.generated.h"

/**
 * 
 */
UCLASS()
class SANDBOX_API UCustomNodes : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

        public:

		UFUNCTION(BlueprintCallable, meta = (DisplayName = "Get Child Classes", Keywords = "Get Child Classes"), Category = Class)
		static TArray<UClass*> GetClasses(UClass* ParentClass);
};


.ccp


// Fill out your copyright notice in the Description page of Project Settings.

#include "Sandbox.h"
#include "CustomNodes.h"

TArray<UClass*> UCustomNodes::GetClasses(UClass* ParentClass)
{
	TArray<UClass*> Results;

	// get our parent blueprint class
	const FString ParentClassName= ParentClass->GetName();
	UObject* ClassPackage = ANY_PACKAGE;
	UClass* ParentBPClass = FindObject<UClass>(ClassPackage, *ParentClassName);

	// iterate over UClass, this might be heavy on performance, so keep in mind..
	// better suggestions for a check are welcome
	for (TObjectIterator<UClass> It; It; ++It)
	{
		if (It->IsChildOf(ParentBPClass))
		{
			// It is a child of the Parent Class
			// make sure we don't include our parent class in the array (weak name check, suggestions welcome)
			if (It->GetName() != ParentClassName)
			{
				Results.Add(*It);
			}
		}
	}
	
	return Results;
}


This gives me a node where I can specify a class as input and it outputs an array of all child classes. Hope this helps other people. This could be improved a lot I guess, so if you have any ideas, please share and I update this.

1c46dde6ca9504d1a1313a10b270773229f81e2a.jpeg

3 Likes

Amazing. I ran into the same need and this was a copy/paste solution. Many thanks @florianbepunkt

There is a function called GetDerivedClasses that can be used precisely for this case and its much less expensive than using the TObjectIterator<UClass>

1 Like