Instantiating Instance of Blueprint Class

I have been hit by this same thing.

What I do in my project is:

  • create a c++ method that I can call in blueprints, that receives a Class as input parameter
  • implementation constructs an object
  • I call the method from blueprints and pass it a proper class.

You might have to be careful about the “outer” in order to avoid having an object have a reference path to a level, which caused me no small amounts of pain in 4.7. You might also have to store it in a UPROPERTY or add it to the Root Set so that it doesn’t get garbage collected.

Example in your header:

UFUNCTION(BlueprintCallable, Category = Character, meta = (DisplayName = "Add weapon with specific class"))
void AddWeapon(TSubclassOf<UWeapon> WeaponClass);

Example in your cpp:

void AYourCharacter::AddWeapon(TSubclassOf<UWeapon> UpgradeClass)
{
    //I assume MyWeapon is a UPROPERTY, you might want to do something different
	MyWeapon = ConstructObject<UWeapon>(WeaponClass, this);
}

In my blueprint class, the method looks like this (I add Upgrades instead of weapons):

37380-ejemplo-uobject.jpg

I have a general variant of this that is a static factory method, and creates objects with the class itself as outer. Works like a charm and objects get garbage collected properly (maybe it is an abuse of my part)