Hi all. I’m using CreateDefaultSubobject to create a component on my character in the constructor. I want to use a TSubClassof variable to change the class type that is spawned, so that I can spawn blueprint classes. An example would be to change one AbilityComponent class in the constructor so that it spawns a different component from the TSubclassof variable, if that makes sense.
So far, I run into error after error after error, with no light at the end of the tunnel. (Errors like “Typed element was requested for before the registry was available!” and IsChildOf(ReturnType) assertion error)
Is what I’m looking for possible to do in the constructor and NOT at runtime? I can use it at runtime but I would like it as a last resort. Thanks.
This sounds like your constructor ran before another which your method depends on. CreateDefaultSubobject always felt a bit hacky to me because you can’t assume that during your class its constructor another class constructor already ran. I do use the UDeveloperSettings class all the time to set default classes in using TSoftClassPtr / TSoftObjectPtr, which I then retrieve in my constructors without errors.
So something like this:
UObject* X = CreateDefaultSubobject(TEXT("X"), YourClass .Get(), YourClass .Get(), true, false, false);
OR
If you are going to do this for many characters or something consider using a datatable instead of the project settings, that should work too. I’ve heard people complain datatables are not accessible in a constructor or BP constructor but that there would be a plugin for that.