Download

Spawn Actor from a plain C++ class.

Hello

I want to spawn a actor from a plain C++ class.
Then I want to initianate that class inside the GameMode in StartPaly();

My plain C++ class the .cpp file is




#include "Building.h"


Building::Building()
{
    FVector NewLocation = FVector(0.f, 0.f, 300.f);


    UE_LOG(LogTemp, Warning, TEXT("Create Building "));

}

void Building::SpawnFirstActor()
{
    World->GetWorld();

    if (World != nullptr)
    {
        World->GetWorld()->SpawnActor<AABuilding>();
        UE_LOG(LogTemp, Warning, TEXT("World Ok"));
    }

}

Building::~Building()
{
    //World->DestroyActor(ActorRef);
}


and the GameMode the .cpp is




#include "TutorialObjectGameModeBase.h"

ATutorialObjectGameModeBase::ATutorialObjectGameModeBase()
{

}

void ATutorialObjectGameModeBase::InitGame(const FString& MapName, const FString& Options, FString& ErrorMessage)
{
    Super::InitGame(MapName, Options, ErrorMessage);
    UE_LOG(LogTemp, Warning, TEXT("Init Game"));
}

void ATutorialObjectGameModeBase::StartPlay() 
{
    Super::StartPlay();

    CBuild = new Building();
    CBuild->SpawnFirstActor();
    //GetWorld()->SpawnActor<AABuilding>();
    UE_LOG(LogTemp, Warning, TEXT("Start Play"));

}


The error that said is Spawn Actor failed because we are running a ConstructionScript.

Why that error?

But that


GetWorld()->SpawnActor<AABuilding>();

line inside the GameMode spawn normally the actor…

What is the difference of two?