Creating an array of classes

I made a Map Spawner class in c++. part of what it uses was an Array of classes that it used to instantiate map pieces using:


where Fchunk was of type UClass* FchunkType;

I wanted to have an array of FchunkTypes, and instantiate and test them.

However I’m doing 99% of my code in blueprint. So I made my map spawner class an actor and made a blueprint that inherited from it. I was planning on adding my map types to a UClass* array and have the code use them.
However I cannot use this as blueprints can’t use UClass. blueprints can however story arrays of class types. So I could have an array of MapChunkBase’s and instantiate from there. However I do not know how to declare an array of class types in c++, only in blueprints?

Does anyone know how to make a class type variable in c++?

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Any)
TArray< TSubclassOf > ArrayOfClasses;

This is how you declare an array of class types in ue4 c++. The base class should be the class all your map types inherit from (preferably you created a base class and inherited from that, you can still have the unreal classes too). You can manipulate this in the editor, blueprints, and c++.

This worked perfectly thank you very much!

How do I then add a class to such array in C++? Let’s say I have something like TArray<TSubclassOf<USkill>> Skills
and a couple of classes (e.g. Skill0, Skill1…) that inherit USkill. I would like to then add Skill0 and Skill1 to the Skills array.

I suppose after this long it may be silly to even ask but have you got this figured out for yourself?

Partially. I managed to do this in blueprints simply by using arrays related nodes on my Skills array. I wasn’t able to do this using cpp though.

Ok so I thought I had a simple answer for you but when I actually looked at my code I believe everything I’m doing with TSubclassOf involves setting default values through a blueprint inherited from my c++ class, and this is probably how that type should usually be used.

That being said I don’t want to assume that this will be enough for you, and I have managed to find another topic that seems to describe dynamically getting a class reference that can be used to populate the value of a TSubclassOf, and from there it should be no problem to add it to an array of the same.

I know this is from 2015 so things may have changed a bit, and it seems a little bit messy to me, but also fairly manageable if you need to be able to populate your TSubclassOf from c++

Sorry I couldn’t give you as clean of an answer as my memory told me I could, but hope this will be helpful to you either way.

Thank you! I’ve been looking for this information for three million years. And thanks to I found it. It is so good that there are people like you !!!