# Why does UE crash when I spawn a tile actor like this?

I'm trying to replicate a version of "Paper.io" using Unreal Engine and C++ as an exercise.

My idea is to have an instance of an Actor (that is my "Grid class" in this case), and another Actor acting like "tiles", so i made another class called "Tile". This is the constructor code of the class "Tile":

ATile::ATile()
{
// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;

if (!RootComponent)
{
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
}

TileMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Tile Mesh"));

static ConstructorHelpers::FObjectFinder<UStaticMesh> PlaneMesh(TEXT("/Engine/BasicShapes/Plane.Plane"));

if (PlaneMesh.Succeeded())
{
TileMesh->SetupAttachment(RootComponent);
TileMesh->SetStaticMesh(PlaneMesh.Object);
}
}


Each time i drag and drop this file into the scene, i can easily see the Plane Mesh in the World, so i believe this is working so far.

Into my Grid class i defined my array in this way:

TArray<ATile*> Grid;


And what i'd like to do now, is to populate this array of Tile's instances. I've been looking around for a solution for tons of time but i still can't figure out how to do it. I would do something like this:

ATile Tile = ATile();


But of course it doesn't work. And i also found this documentation: Spawning Actors

I tried to do a similar thing, but since i'm a beginner, everything exploded. I don't really get an error, UE4 just crash, but Visual Studio still compile it.

void AGrid::CreateGrid()
{
for (int x = 0; x <= 5; x++)
{
for (int y = 0; y <= 5; y++)
{
ATile* Tile = GetWorld()->SpawnActor<ATile>(ATile::StaticClass());
}
}
}


How could i make this work? Am i doing something wrong?

EDIT 1: I added the suggested line of code, but it still keep crashing.

Access violation - code c0000005 (first/second chance not available)

ATile* Tile = GetWorld()->SpawnActor<ATile>(GetClass());


The parameter to this overload of SpawnActor is the type of the actor to spawn. You're calling GetClass() on the AGrid, but trying to cast the resulting AGrid actor to an ATile. This is legal C++, which is why it compiles; it will fail at runtime.

You want:

ATile* Tile = GetWorld()->SpawnActor<ATile>(ATile::StaticClass());


(e.g., the type of the thing you want to create, not the type of the thing creating it)

Based on your edit, it's also apparent that you're calling CreateGrid() inside the constructor for AGrid, where GetWorld() may (and does, in the case of the class default object that you're constructing -- the UClass::CreateDefaultObject call on the stack) return a null pointer.

You should probably call CreateGrid during the BeginPlay overload of the AGrid actor, or a similarly later call (such as OnConstruction), depending on your specific needs.

You could also just check the result of GetWorld(), and if it is null, skip the spawn attempt.

• That solves the crash, but my Grid's array is still empty in the editor. Also, i wanna create my grid before the BeginPlay(). – user3287178 Dec 6 '17 at 20:29
• Empty in the editor where? UE's object lifetime is complicated. You can consider OnConstruction or PostLoad functions if BeginPlay doesn't work for you. But you can't do anything that requires a World pointer in your constructor unless you guard against the CDO case. – Josh Dec 6 '17 at 20:32
• I'm using an IF condition inside the constructor to check GetWorld(), and seems to work actually. if (GetWorld()) CreateGrid(); – user3287178 Dec 6 '17 at 20:36
• Yes, that can work, if you're careful. The nature of UE's object system is that a lot of assumptions you might normally have don't always hold in constructors with respect to the state of the object graph, so you need to be careful in there. Places like PostLoad or OnConstruction are generally safer or at least easier to work with, and BeginPlay is where you'd want to do pre-runtime sort of work. – Josh Dec 6 '17 at 20:40
• Ok, thank you so much for your help and tips. I appreciate it. – user3287178 Dec 6 '17 at 20:48