# Assigning TSubClassOf in C++

**URL:** <https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [February 24, 2015, 1:33pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925 "2015-02-24T13:33:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_66a0fcb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_66a0fcb6](https://forums.unrealengine.com/u/anonymous_user_66a0fcb6)\
**Post date:** [February 24, 2015, 1:33pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/1 "2015-02-24T13:33:30Z")

</div>

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.

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — https://unrealcommunity.wiki/! You will be able to find content from...

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`.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_957a4452](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_957a4452](https://forums.unrealengine.com/u/anonymous_user_957a4452)\
**Post date:** [February 24, 2015, 1:56pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/2 "2015-02-24T13:56:10Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_66a0fcb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_66a0fcb6](https://forums.unrealengine.com/u/anonymous_user_66a0fcb6)\
**Post date:** [February 24, 2015, 2:20pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/3 "2015-02-24T14:20:43Z")

</div>

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.  
▼

---

<div class="post-metadata">

**Author:** ![kamrann](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@kamrann](https://forums.unrealengine.com/u/kamrann)\
**Post date:** [February 24, 2015, 2:24pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/4 "2015-02-24T14:24:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_741503d3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_741503d3](https://forums.unrealengine.com/u/anonymous_user_741503d3)\
**Post date:** [February 24, 2015, 2:32pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/5 "2015-02-24T14:32:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_66a0fcb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_66a0fcb6](https://forums.unrealengine.com/u/anonymous_user_66a0fcb6)\
**Post date:** [February 24, 2015, 3:12pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/6 "2015-02-24T15:12:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_66a0fcb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_66a0fcb6](https://forums.unrealengine.com/u/anonymous_user_66a0fcb6)\
**Post date:** [February 24, 2015, 3:24pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/7 "2015-02-24T15:24:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_957a4452](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_957a4452](https://forums.unrealengine.com/u/anonymous_user_957a4452)\
**Post date:** [February 24, 2015, 3:41pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/8 "2015-02-24T15:41:55Z")

</div>

Try to use this

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

```

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_66a0fcb6](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_66a0fcb6](https://forums.unrealengine.com/u/anonymous_user_66a0fcb6)\
**Post date:** [February 24, 2015, 3:55pm UTC](https://forums.unrealengine.com/t/assigning-tsubclassof-in-c/307925/9 "2015-02-24T15:55:29Z")

</div>

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.
