Dynamic object creation having it's UClass

Hello guys, I need your help.
Sorry for tl;dr, you can jump to questions at the bottom.

I’m working on skill system, each skill is based on object (not an actor, I don’t want them to be spawned on level, they will be more like handlers spawning some particles etc.).
I want to make it easy to use from blueprint (some skills will definitely use this code). Currently I have BaseSkill class that will be used as base by others. Inside a blueprint I want to set class variable (with function taking UClass parameter) in character which will be used to decide which skill can player use. This works fine.
The problem starts when I want to dynamically create skill object without knowing its class, this is just UClass put into variable. I tried some casting, dynamic objects loader, but they just gave me editor crash. I wanted to use also UClass::CreateDefaultObject() but its protected, I don’t think I should change it to public and then changing lot of code in child classes (unresolved exceptions).

So, 2 questions:

  1. What is the best way to create objects dynamically, knowing only its UClass?
  2. How can I force variable in C++ to store specific class (or it’s children), not any class like UClass does. In blueprint it’s easy, just setting variable type…

Ok, i figured out 1st question, this: ConstructObject<UBaseSkill>(classParameter, this); worked… As far as I remember I tried something like this, but this time this works… Then encountered more errors with world context (no idea why UObject dont use this… it crashed me a lot).

Still hoping to see advice about question 2 :slight_smile:

You can use TSubClassOf to force a variable to be a child class of a specified class. For example:

TSubClassOf<UBaseSkill> SkillClass;

This is exactly what I needed. Thank you very much!