Is it possible to assemble a list of classes of a type at runtime? (BP or C++)

I am working on a system to spawn random actors and thus need a random class. I would like to avoid making a array/map/etc of every class as they are created because it’s fairly easy to make mistakes with (e.g., forgetting to add one.) I had the idea to use a program to generate a table by scanning all the files, but that would rely on being able to get a class by name which I couldn’t find anything about being able to do it.

Edit for some clarity of what I am trying to accomplish:

Something similar to this, but instead of map names it provides class types.

I am creating an endless side scrolling shooter that spawns a random tile as you move forward. I plan on having many different types of tiles and as such I will have many blueprints subclassing e.g. TileBase. As far as I can find the only way to get a random class to spawn is by creating an array of the type of tile classes - thus every time I make a new tile I need to manually add a new entry into the array (this is, for example, how it’s accomplished in the Infinite Runner tutorial.) There are ways to optimize this, of course, but that’s beside the point.

My current solution is to use a data table. It has the advantage of being able to pair the class with a gameplay tag container to more easily filter the tiles to randomly choose from. As stated before, however, it opens up the issue of forgetting to add new entries to the table.

Each class has corresponding UClass object in memory, so all you need to do is iterate theu them using TObjectIterator, filter them and add matching once to array or whatever you want

TArray<UClass*> Classes;

for (TObjectIterator<UClass> It; It; ++It)
{
      if(It->IsChildOf(ABaseClassThatWeLookingFor::StaticClass())) {
             Classes.Add(*it);
     }

}

Problem is it is expensive process even heavier then actor iteration with Get All Actors of Class, so you should do this once, in beginplay for example

In blueprint it impossible… i think? if you can iterate objects you should be able to. Generly it would be better if you do this in C++ for performance and you can also re use array created in C++ in blueprint.

I agree - do it once and store the results before anything requiring responsive gameplay happens during runtime.

Is this generating a list of all classes, or a list of classes that are in use?

And yeah, this is definitely something that would be cached. I’m just trying to avoid needing to manually add a new entry to a static array every time I make a new child.

It will list all UClass in memory, this means all UClass of all loaded modules and Enigne module alone which is loaded all the time in all games, Sadly this is only way to list classes that are in memory as C++ is low level language and once compiled any class information is lost, and engine don’t track classes used (atleast i didnt find any case of that, engine is so huge you always find something new about it everyday). and it frequently used methods also in engine code, if you search github by TObjectIterator<UClass> you will find countless examples of use, you might also find methods use to optimize it in them.

But since you want to list classes “in use” there is method to do so, by making objects that you spawn register the class in the array. This method is used to register created perticilar objects so there no need to search for them in memory which is heavy process. But this can be used to register used classes too. You can only do this when object is placed in editor (actor code is active then too) or on BeginPlay, so objects actully have a chance to do so.

Just make SomeGameModePointer->SomeClassArray.AddUnique(GetClass()); and it will gather all classes that been spawned, you would also need extra code to delete class from array, that might would require some actor counting and invidual object listing

You misunderstand me. Right now when I create a new child class blueprint I have to manually add it’s class type to a data table, then when I spawn a class I pick random entry from that table.

As stated in my original post, I’m not looking for a list of classes in memory, I’m looking for a list of all the possible child classes. Since I’m trying to avoid needing to manually make the array, the array would have to be made at run time.

I wonder if the AssetManager class could help

I looked into it but it’s not really getting me anywhere. Seems like that is mostly for getting assets available, so I still wouldn’t have a list of class objects from which to spawn classes.