Assigning TSubClassOf in C++

Hi,

I’m doing some modifications on the First Person Example project and I want to assign/change the type of projectile that is used in code.

The variable is of type TSubclassOf. I’m using the code found here to get a reference to the blueprint of the projectile I want to use.

So in my function I have something like this:

void ACPP_FPS_LearnCharacter::SetProjectile(FName AssetPath) { ProjectileClass = *LoadObjFromPath<ACPP_FPS_LearnProjectile>(AssetPath); }

However the error I get is that ProjectileClass is of type TSubclassOf and the return value is of type ACPP_FPS_LearnProjectile so the assignment fails. I can just use a regular pointer but I was wondering how to do that with TSubclassOf.

I’m not sure about this, but I think your issue may be that your code is attempting to load an object of type ACPP_FPS_LearnProjectile, whereas what you want to do is load the blueprint/class itself. Try the following (note the omission of the * you had):

ProjectileClass = LoadObjFromPath<UClass>(AssetPath);

You may perhaps need to edit your asset path by appending ‘_C’ to the end of it, I’m not sure.

You could perhaps try UBlueprint instead of UClass too, however I think I read somewhere that this use was being phased out.

Hi Swiftle,

Try to use:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Options)
TSubclassOf<class AProjectileBase> ProjectileClass;

In this case, you can setup ProjectileClass in blueprint to all blueprints or classes derived from AProjectileBase.

Hope it helps!

Hello,

I know I can do that in Blueprints but I actually want to do it in C++. I want to be able to switch my projectiles. Let’s say I have a bouncing projectile and an explosive projectile. I want to be able to change between them and I really wanted to write the logic in C++. I’ve actually tried to replace TSubclassOf<> with a simple pointer to the the class but the I when I try to spawn it nothing happens. The SpawnActor in my Fire() function wont recognize the UClass* reference.

Again this is all done with the C++ First Person Shooter example in UE4. I haven’t added any new classes. Oh and btw I’ve tried to do it with the use of ConstructorHelpers::FObjectFinder but I can only call that function in the class constructor which is rather limiting.

Have you tried LoadClass() instead of LoadObjFromPath()?

Is that the correct syntax:
ProjectileClass = LoadClass < MyProjectile > (NULL, TEXT("Blueprint'/Game/Blueprints/Projectile.Projectile'"), NULL, LOAD_None, NULL); ?

I tried what you suggested but the ProjectileClass remains empty after the assignment. Also using UBlueprint instead of UClass results in an error about incorrect assignment.

Try to use this

 auto pObj = LoadObjFromPath<ACPP_FPS_LearnProjectile>(AssetPath);
    if (pObj)
       ProjectileClass = pObj->GetClass();

Guys thank you all for your help I figured it out. The problem is the string that I was using, as kamrann suggested I have to add a _C. For example, my original blueprint reference was this: Blueprint'/Game/Blueprints/Projectile.Projectile'. However this would always assign NULL to my Projectile Class. However once I changed the string to /Game/Blueprints/Projectile.Projectile_C and used LoadClass, everything worked correctly and the Blueprint was assigned. My guess is that the first string is a reference to a UBlueprint and I actually need the UClass from that UBlueprint.