UE 4.6 Blueprint Spawning using C++

Hello,

First, I’d like to tell you I’ve been searching a solution for this issue in the past couple hours. All the information I could find was either outdated (prior to UE 4.6) or didn’t solve my issue.
I apologize in advance if this is a simple issue, but I’m really struggling to make this work.

Okay, lets get to it: I’m having trouble to spawn Blueprints via C++.

I am able to create a class in C++, create a blueprint based off it and drag and drop it into the map. This is easy, it works, no issues here.

But I what I want (and currently unable to do) is:

  1. Create a class using C++. Lets
    call it ABarrel.
  2. Create a Blueprint based on
    ABarrel class.
  3. Edit properties of the class
    using blueprints (The Static Mesh
    one is a nice one to start, since it
    gives you visual feedback, but I
    tried with a simple Boolean to make
    sure the problem wasn’t with the
    mesh)
  4. Replace the
    AGameCharacter::OnFire() function
    with a simple blueprint instance
    spawn.

After a lot of trial an error, I’m able to spawn the ABarrel class, but without any modifications from the blueprint.
This is the code for it:

On GameCharacter.h, inside the character class

	UPROPERTY(EditDefaultsOnly, Category = Barrel)
	TSubclassOf<class ABarrel> ManipulatedBarrel;

And on GameCharacter.cpp

void AGameCharacter::OnFire()
{
	UWorld* const World = GetWorld();
	if (World != NULL)
	{
		FVector SpawnLocation (0, 0, 450);
		FRotator SpawnRotation(0, 0, 0);

		ManipulatedBarrel = ABarrel::StaticClass;
		World->SpawnActor<ABarrel>(ManipulatedBarrel, SpawnLocation, SpawnRotation);
	}

}

Sadly, it does not get any of the Blueprint data.
I’ve tried to play with a LOT of functions, with no success… For example, inside my barrel class, there is:

	static ConstructorHelpers::FClassFinder<AActor> BarrelResource(TEXT("/Game/Blueprints/Barrels/BP_Barrel"));
	if (BarrelResource.Class != NULL)
	{
		m_BPBarrel = BarrelResource.Class;
	}

where I have in the header

TSubclassOf<AActor> m_BPBarrel;

But it does not work.

I’m stuck. How can I effectively spawn my barrel (eventually I’ll need many barrels) that gets the properties from Blueprints I modified in the editor?

If I have to choose, I really prefer to avoid defining these in the ABarrel constructor, since I would love to have a single Barrel class and be able to pick one out of many Barrel blueprints and spawn it.
If that’s not possible, I could create many classes that inherit from ABarrel to have one specific blueprint for each.

Thanks in advance :slight_smile:

I ran into a similar problem a couple weeks ago. My recommended solutions are:

Get rid of Blueprints and do everything in C++

OR

Make a BlueprintImplementableEvent on your C++ class that you want to do the spawning. Have this take any parameters you need to know which Blueprint to spawn (I’m using an id system). Then, create a Blueprint that extends your C++ class and defines the behavior of the event (this includes doing the spawning). Since you’re now spawning within Blueprint, you’ll have access to the other Blueprints.

OR

What you’re trying to do now. This is also what I started with, but I think it’s a relatively poor solution. You end up having to bake blueprint information into your final game binary that you normally wouldn’t have to include. This solution also seems the least maintainable.

Hey @lojo,

Do you mind explaining a bit more how you did the BlueprintImplementableEvent trick, please? :slight_smile: