How to get a derived class from TSubclassOf?

I Have a pointer like this:

UVehicleWheel* Wheel = VehicleSimComponent->WheelSetups[WheelSim.WheelIndex].WheelClass;

WheelSetups is an array of USTRUCT, witch holds WheelClass as a UPROPERTY TSubClassof < UVehicleWheel >

Now, I want to get a derived class from UVehicleWheel, let’s say it is UMyVehicleWheel, and access a method (function) within it. But how?

First of all TSubclassOf is template which just limits selection in editor (and i think in C++ too) to specific class relation, what you dealing here is UClass class, so all functions you can find in API refrence of UClass:

UClass class is part of reflection system when engine is starting one of things it does at begining is creating map o itself by creating UClass, UStruct, UProperty etc all UField’s objects containing information about code structure so engine can see how it is build, it like looking on mirror thats why it’s called reflection system.

EDIT whoops i misundersttod a quastion ^^’

Now, I want to get a derived class
from UVehicleWheel, let’s say it is
UMyVehicleWheel, and access a method
(function) within it. But how?

You can’t, because you got UClass here, you need first spawn object of that class and then you can access code of it, optionaly you could try to get CDO (Class Default Object) to read default properties values, you can get it with this method:

But you should only use that for that, engine use CDO to spawn new objects if you modify anything in to it the new objects will be spawn as modified and objects spawning in general might start to act wierd. So read only here.

Now i assume you also dont know how to cast, if you want to change type of varable you cast like this

UVehicleWheel* YYY;
UMyVehicleWheel* XXX =  Cast<UMyVehicleWheel>(YYY);

And this is only for UObjects and UE4, nativly in C and C++ you cast this way

UVehicleWheel* YYY;
UMyVehicleWheel* XXX =  (UMyVehicleWheel*)YYY;

This will work too but it unsave if YYY wont be UMyVehicleWheel then you gonna have crash you would need to check if it really that class first, Cast function do that check for you and if it not right class it will return null so it’s safer and less ugly

But again you need object of a class first UClass is just class identifier

Thank you very much for your answer!

Your explanation is very clear, thanks. Actually, I already tried to Cast using the first method that you described here, but I got several linker errors - unresolved external symbol. When I get back home, will try the second one and see what I can do.

Cast should work… unresolved external symbol means either you missing code in cpp for declered function in header file or linker can’t find binery code to link to.

I’ll try to get this cast to work, it’s the only option that I can use …

Have found what caused linker error, I was adding “AnimGraph” as module dependency, but it should be “AnimGraphRuntime”… Now I can’t access a different class, and I think it’s same problem… FPhysXVehicleManager , and it seams that including PhysX and APEX as dependency modules doesnt resolve it… stuck again

EDIT: Oh, and Cast finally worked! Thanks again ^^!