Find all subclasses of a certain baseclass?

I have an item-baseclass that is probably going to have a lot(100+) of subclasses. Is there a way to automatically get a reference to every subclass, so that I can spawn a random one out of that list? Or will I have to manually include/add all the subclasses in a spawner class?

Thanks in advance for any help :slight_smile:

Manually create array of subclasses on init, even engine does that:

https://github.com/EpicGames/UnrealEngine/blob/a27ad66f0a075f3b74ef8f68a4b2b0da4882425e/Engine/Source/Editor/UnrealEd/Private/SoundCueGraphSchema.cpp#L492

So it loks like this:

for(TObjectIterator<UClass> It; It; ++It)
{
	if(It->IsChildOf(USomeAwesomeClass::StaticClass()) && !It->HasAnyClassFlags(CLASS_Abstract))
	{
		Subclasses.Add(*It);
	}
}
2 Likes

Thank you very much, that is excatly what I have been searching for. Didn’t know it would be as simple as just running an iterator over UClass, but hey, the simpler the better :slight_smile:

Here’s another way of doing it, for those coming in off google:

	TArray<UClass*> OutClasses;
	GetDerivedClasses(UBaseClass::StaticClass(), OutClasses, true /* bRecursive */);
1 Like