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:
- Create a class using C++. Lets
call it ABarrel. - Create a Blueprint based on
ABarrel class. - 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) - 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