How do I create a subobject from a variable class

Basically something like CPP File · GitHub

The best way to do this in UE is via the editor. Select File->Add Code To Project… and then select the class you would like to extend from the list (note: you may have to check the box which shows all available classes). UE4 will then generate all the necessary headers and includes for you.

Thats not what im asking, I want the Class used in CreateDefaultSubobject to come from a variable, or a different way to add the subobject where the class can be dynamic.

The idea here is that there will be many different characters that all descend from ASubCharacter, and all of those characters will have a BasicAttack component that handles the API, but that BasicAttack component will be a different class inheriting from UCharacterAttack on every character.

Maybe I’m missing something, but it looks like that is what your code is already doing?

Thats what my code is trying to do, but it doesnt compile. It doesnt like the

<BasicAttackClass>

Does GetDefaultSubobject create the component and attatch it to the character? My UCharacterAttack’s are components becuase they need to tick and they handle some input.

Ok, yeah you can’t use BasicAttackClass in that way. Try something like this:

In your header:

UCharacterAttack* BasicAttack;

In your cpp:

BasicAttack = BasicAttackClass.GetDefaultSubobject();

I’m afraid I don’t know. I doubt it, but I don’t know the API well enough to say, I’m sorry.

Thanks, I’ve changed UCharcter attack to descend from UObject and FTickableGamObject and everything seems to be working now, I just don’t get the pretty variables in the “Defaults” list in the editor to play with.