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?
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.
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.
There is a function called GetDerivedClasses that can be used precisely for this case and its much less expensive than using the TObjectIterator<UClass>