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