Spawning Actors from an Array Causes a Crash

Running this

void AEnShips::BeginPlay()
{
    	Super::BeginPlay();
    	//swarm[0][0] = mySpawn(0, 0, FVector(100, 0, 100), 1);
    	//swarm[0][1] = mySpawn(0, 1, FVector(200, 0, 200), 2);
    	//swarm[1][0] = mySpawn(1, 0, FVector(300, 0, 300), 3);

	swarm[0][0] = mySpawn(0, 0, FVector(0 * 100, 0, 0 * 100), 1);
	swarm[0][1] = mySpawn(0, 1, FVector(0 * 100, 0, 1 * 100), 2);
	swarm[0][2] = mySpawn(0, 2, FVector(0 * 100, 0, 2 * 100), 2);
}

// Called every frame
void AEnShips::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

AEnemyShip* AEnShips::mySpawn(int x, int y, FVector pos, int size)
{
	AEnemyShip* a;
	if (size == 1)
		a = ()->SpawnActor<ASmallEnemyShip>(ASmallEnemyShip::StaticClass());
	else if (size == 2)
		a = ()->SpawnActor<AMedEnemyShip>(AMedEnemyShip::StaticClass());
	else if (size == 3)
		a = ()->SpawnActor<ABigEnemyShip>(ABigEnemyShip::StaticClass());
	else
		a = ()->SpawnActor<AEnemyShip>(AEnemyShip::StaticClass());
	a->x = x;
	a->y = y;
	a->SetActorLocation(pos);
	return a;
}

Causes this error and a crash:

MachineId:B8E3D5B0468E0CA8A91B8396E4F7C920
EpicAccountId:269059384fbc4ad38574b0a2b834709a

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

""

UE4Editor_SpaceInvaders_5426_Win64_DebugGame!AEnShips::mySpawn() [c:\users\\onedrive\documents\unreal projects\spaceinvaders\source\spaceinvaders\enships.cpp:63]
UE4Editor_SpaceInvaders_5426_Win64_DebugGame!AEnShips::BeginPlay() [c:\users\\onedrive\documents\unreal projects\spaceinvaders\source\spaceinvaders\enships.cpp:27]
UE4Editor_Engine!AWorldSettings::NotifyBeginPlay() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\worldsettings.cpp:132]
UE4Editor_Engine!AGameMode::HandleMatchHasStarted() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\gamemode.cpp:605]
UE4Editor_Engine!AGameMode::SetMatchState() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\gamemode.cpp:721]
UE4Editor_Engine!AGameMode::StartMatch() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\gamemode.cpp:582]
UE4Editor_Engine!UWorld::BeginPlay() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\world.cpp:3050]
UE4Editor_Engine!UGameInstance::StartPIEGameInstance() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\engine\private\gameinstance.cpp:273]
UE4Editor_UnrealEd!UEditorEngine::CreatePIEGameInstance() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\editor\unrealed\private\playlevel.cpp:3137]
UE4Editor_UnrealEd!UEditorEngine::PlayInEditor() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\editor\unrealed\private\playlevel.cpp:2385]
UE4Editor_UnrealEd!UEditorEngine::StartQueuedPlayMapRequest() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\editor\unrealed\private\playlevel.cpp:1066]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\editor\unrealed\private\editorengine.cpp:1244]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\editor\unrealed\private\unrealedengine.cpp:366]
UE4Editor!FEngineLoop::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\launch\private\launchengineloop.cpp:2428]
UE4Editor!GuardedMain() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\launch\private\launch.cpp:142]
UE4Editor!GuardedMainWrapper() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.9\engine\source\runtime\launch\private\windows\launchwindows.cpp:200]

But this is fine:

void AEnShips::BeginPlay()
{
	Super::BeginPlay();
	swarm[0][0] = mySpawn(0, 0, FVector(100, 0, 100), 1);
	swarm[0][1] = mySpawn(0, 1, FVector(200, 0, 200), 2);
	swarm[1][0] = mySpawn(1, 0, FVector(300, 0, 300), 3);

	//swarm[0][0] = mySpawn(0, 0, FVector(0 * 100, 0, 0 * 100), 1);
	//swarm[0][1] = mySpawn(0, 1, FVector(0 * 100, 0, 1 * 100), 2);
	//swarm[0][2] = mySpawn(0, 2, FVector(0 * 100, 0, 2 * 100), 2);
}

// Called every frame
void AEnShips::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

AEnemyShip* AEnShips::mySpawn(int x, int y, FVector pos, int size)
{
	AEnemyShip* a;
	if (size == 1)
		a = ()->SpawnActor<ASmallEnemyShip>(ASmallEnemyShip::StaticClass());
	else if (size == 2)
		a = ()->SpawnActor<AMedEnemyShip>(AMedEnemyShip::StaticClass());
	else if (size == 3)
		a = ()->SpawnActor<ABigEnemyShip>(ABigEnemyShip::StaticClass());
	else
		a = ()->SpawnActor<AEnemyShip>(AEnemyShip::StaticClass());
	a->x = x;
	a->y = y;
	a->SetActorLocation(pos);
	return a;
}

What am I doing wrong? I’m a bit of a noob to this, but I feel like both should work. My array was declared like this:

AEnemyShip* swarm[10][6];

So I should have enough room…

Any ideas?

Array is not a fault, looking on stackdump:

UE4Editor_SpaceInvaders_5426_Win64_DebugGame!AEnShips::mySpawn() [c:\users\\onedrive\documents\unreal projects\spaceinvaders\source\spaceinvaders\enships.cpp:63]
 UE4Editor_SpaceInvaders_5426_Win64_DebugGame!AEnShips::BeginPlay() [c:\users\\onedrive\documents\unreal projects\spaceinvaders\source\spaceinvaders\enships.cpp:27]

Your code crashes in mySpawn, i susspecting here:

   a->x = x;
    a->y = y;

If ship fail to spawn it will be null so if you call something from it will crash in that case

So do this:

if(a) {
     a->x = x;
     a->y = y;
     a->SetActorLocation(pos);
}

Now if spawn fail it should not crash and in logs you can check why they didn’t spawn SpawnActor prints reason in all cases

Other reason why you could get crash is wrong type casts incompatible with AEnemyShip*

I gave this a shot and it worked, my editor no longer crashes. However, only one of the AActors (the first one) spawned. Any ideas?

Got it. I needed to use SpawnActorDeferred instead of SpawnActor. Not really sure why. I think it’s because I construct the Actors elsewhere, before spawning, but idk where. Whatever…

Correct code:

void AEnShips::mySpawn(int x, int y, FVector pos, int size)
{
	if (size == 1)
		swarm[x][y] = ()->SpawnActorDeferred<ASmallEnemyShip>(ASmallEnemyShip::StaticClass(), FTransform(pos), nullptr, nullptr, ESpawnActorCollisionHandlingMethod::Undefined);
	else if (size == 2)
		swarm[x][y] = ()->SpawnActorDeferred<AMedEnemyShip>(AMedEnemyShip::StaticClass(), FTransform(pos), nullptr, nullptr, ESpawnActorCollisionHandlingMethod::Undefined);
	else if (size == 3)
		swarm[x][y] = ()->SpawnActorDeferred<ABigEnemyShip>(ABigEnemyShip::StaticClass(), FTransform(pos), nullptr, nullptr, ESpawnActorCollisionHandlingMethod::Undefined);
	else
		swarm[x][y] = ()->SpawnActorDeferred<AEnemyShip>(AEnemyShip::StaticClass(), FTransform(pos), nullptr, nullptr, ESpawnActorCollisionHandlingMethod::Undefined);
	if (swarm[x][y]){
		swarm[x][y]->x = x;
		swarm[x][y]->y = y;
		swarm[x][y]->SetActorLocation(pos);
	}
}