I have a blueprint-based actor that I want to spawn from a C++ class.
In the class declaration, I have a private variable:
UClass* APlayerCharacter;
Then in the constructor I have:
static ConstructorHelpers::FObjectFinder<UClass> PlayerBP(TEXT("Blueprint'/Game/Entities/PlayerCharacter.PlayerCharacter'"));
if(PlayerBP.Succeeded()) { APlayerCharacter = PlayerBP.Object; }
else { UE_LOG(LogTemp, Error, TEXT("AMapData Failed to find Player class.")); }
This compiles fine, but when I try to open the project in UE4 it hangs at 70%.
My intended use for this thing is a call like this:
()->SpawnActor<AActor>(APlayerCharacter->StaticClass(), transform, parameters);
I’ve been many threads and questions on this topic but I can’t figure out what I’m doing wrong here.
Any guidance would be really seriously appreciated at this point.
APlayerCharacter is a UClass object, so with you use StaticClass (btw you should use GetClass on objects insted) it will return UClass od UClass which is not a actor, so remove that part “->StaticClass()” APlayerCharacter is alreasy UClass
Still this not explains why you get crash on engine load, that spawnactor would simply not work and dont spawn anything. Crashes on engine load usally means the error is in constrctor and engine crashes when it creates class default object where constructor is interacted for the first time. Can you paste full constructor? i hope you not trying to spawn in constructor Also check the logs in Saved/Logs in p[roject director there should be crash info there, if you see incomplere log like it was stop in middle that means there call on null pointer somewhere and OS stopped engine due to error in your code
Okay. I managed to get it working after finding a forum post that had the same issue.
I changed the call to this:
ConstructorHelpers::FObjectFinder<UClass> PlayerBP(TEXT("Class'/Game/Entities/PlayerCharacter.PlayerCharacter_C'"));
So that it’s fetching the class directly instead of trying to go through the blueprint.
Hey -
I tried copying the code you provided into a project and while I did not get a hang when launching the project, I did get a “CDO Constructor” issue. I was able to work around this error by changing < UClass>
to < UBlueprint>
. Let me know if this change helps.
Cheers
Apparently the hanging was being caused by bDontLoadBlueprintOutsideEditor?
I was after the class, though, so I changed the approach a little as described in comment to OP and it worked. I also removed the ‘static’ specifier from the call, which was how I also started getting CDO errors and was able to find the answer from searching those.
To anyone still struggling with this, I resolved a similar issue by removing the “static” specifier from the call. Its worth noting that i was making a slightly different call though: mine was:
static ConstructorHelpers::FObjectFinder<UBehaviorTree>BehaviorTreeRef(TEXT("the path"));