No luck, my code is currently in AMyGameModeBase::BeginPlay which is my custom gamemode, i can see the “BP NOT found” message, so it is set in the world settings to use this gamemode class.
Is this maybe to early or to late to load actors?
Also the actors path is shown in this popup, just recently saw the /Game in there.
void AMyGameModeBase::BeginPlay() {
Super::BeginPlay();
static ConstructorHelpers::FClassFinder<AActor> BlueprintActor(TEXT("/Game/MyActor2_BP"));
if (BlueprintActor.Succeeded())
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("BP loaded"));
}
}
else
{
if (GEngine)
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("BP NOT found"));
}
}
}
As the name implies ConstructorHelpers need to be in the constructor. You are invoking the command in the wrong place of your code, that is why it fails.
GameMode also doesn’t have a normal begin play anyway.
You need to call it from an actor’s CDO
If you have the class AMyActor then you need to call it in the cpp in
AMyActor::AMyActor(){
// -> call your function here.
static ConstructorHelpers::FClassFinder<AActor> BlueprintActor(TEXT("/Game/MyActor2_BP"));
if (BlueprintActor.Succeeded())
{
// rest of code
}
}
but as the next poster in that thread pointed out TryLoadClass() needs some parameter.
Sadly my Visual Studio Code does not display proper context help, have to download the UE sourcecode to figure this out.
started a new fresh project, just in case. firstActor should spawn secondActor, both are C++ classes as shown in the screenshot and they should exist in /Script
Thank you very much 3dRaven for that complete example, added some more lines of code and now finally a blueprint actor is spawning from c++ code. And thanks to the other contributors as well.
After the next question, which will come, let me buy you a paypal coffee.