[Solved]Possession Error of second player in c++

Hey guys I’m having problems trying to possess the second player in my game. I was able to do this in blueprints with no problem but I decided to go full c++(refusing to use bp) and I got everything working except for this.
The end of the code is pretty much what I need help with.

// Fill out your copyright notice in the Description page of Project Settings.

#include "ProjectBlue.h"
#include "GM_DeathMatch.h"
#include "SpawnLocations.h"
#include "MyCharacter.h"

AGM_DeathMatch::AGM_DeathMatch(const FObjectInitializer &Objectinitializer) :Super(Objectinitializer)
{
	static ConstructorHelpers::FObjectFinder<UBlueprint> PlayerOB(TEXT("Blueprint'/Game/Player/Player'"));
	if (PlayerOB.Object)
	{
		PlayerBP = (UClass*)PlayerOB.Object->GeneratedClass;
	}
}

void AGM_DeathMatch::BeginPlay()
{
	for (TObjectIterator<ASpawnLocations> Spawners; Spawners; ++Spawners)
	{
		const FBox box = Spawners->GetComponentsBoundingBox();
		FVector point = FMath::RandPointInBox(box);
		SpawnLocations.Add(point);
	}
	UGameplayStatics::CreatePlayer(GetWorld(), -1, false);
	FRotator spawnRotation = FRotator(0.0f, 0.0f, 0.0f);
	Index = FMath::RandRange(0, SpawnLocations.Num()-1);
	FVector randChoice = SpawnLocations[Index];
	FActorSpawnParameters SpawnParams;
	SpawnParams.Owner = this;
	SpawnParams.Instigator = Instigator;
	UWorld* const World = GetWorld();
	if (World)
	{
		AMyCharacter* Character = World->SpawnActor<AMyCharacter>(PlayerBP, randChoice, spawnRotation, SpawnParams);
		Character->PIndex = 0;
		SpawnLocations.RemoveAt(Index, 1, true);
		for (int32 i = 0; i < SpawnLocations.Num()-1; i++)
		{
			int32 firstIndex = FMath::RandRange(0, SpawnLocations.Num()-1);
			int32 secondIndex = FMath::RandRange(0, SpawnLocations.Num()-1);
			int32 spareIndex;
			if (firstIndex != secondIndex)
			{
				spareIndex = firstIndex;
				SpawnLocations[firstIndex] = SpawnLocations[secondIndex];
				SpawnLocations[secondIndex] = SpawnLocations[spareIndex];
			}
			

		}
		Index = FMath::RandRange(0, SpawnLocations.Num() - 1);
		FVector randChoice = SpawnLocations[Index];
		AMyCharacter* Character2 = World->SpawnActor<AMyCharacter>(PlayerBP, randChoice, spawnRotation, SpawnParams);
		APlayerController* firstController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
		APlayerController* secondController = UGameplayStatics::GetPlayerController(GetWorld(), 1);
		firstController->Possess(Character);
		secondController->Possess(Character2);
	}
}

If I take out “secondController->Possess(Character2);” I’m able to control the first player but if I leave it in it throws one of those exceptions error in visual studio.

In bp I had it searching for “all actors of class” and then possessing them but idk how to do that in c++ when the player itself is a blueprint (I know how to get the reference to bp through code but don’t know how to search through all of them).

The problem looks like the index for the player controller is not available or something like that for the second player. How would I fix this?

Btw it’s for splitscreen.

PS I’m sorta new-ish to c++ atleast when it comes to UE4

Edited Code:

// Fill out your copyright notice in the Description page of Project Settings.

#include "ProjectBlue.h"
#include "GM_DeathMatch.h"
#include "SpawnLocations.h"
#include "MyCharacter.h"

AGM_DeathMatch::AGM_DeathMatch(const FObjectInitializer &Objectinitializer) :Super(Objectinitializer)
{
	static ConstructorHelpers::FObjectFinder<UBlueprint> PlayerOB(TEXT("Blueprint'/Game/Player/Player'"));
	if (PlayerOB.Object)
	{
		PlayerBP = (UClass*)PlayerOB.Object->GeneratedClass;
	}
}

void AGM_DeathMatch::BeginPlay()
{
	for (TObjectIterator<ASpawnLocations> Spawners; Spawners; ++Spawners)
	{
		const FBox box = Spawners->GetComponentsBoundingBox();
		FVector point = FMath::RandPointInBox(box);
		SpawnLocations.Add(point);
	}
	UGameplayStatics::CreatePlayer(GetWorld(), 1, true);
	FRotator spawnRotation = FRotator(0.0f, 0.0f, 0.0f);
	Index = FMath::RandRange(0, SpawnLocations.Num()-1);
	FVector randChoice = SpawnLocations[Index];
	FActorSpawnParameters SpawnParams;
	SpawnParams.Owner = this;
	SpawnParams.Instigator = Instigator;
	UWorld* const World = GetWorld();
	if (World)
	{
		AMyCharacter* Character = World->SpawnActor<AMyCharacter>(PlayerBP, randChoice, spawnRotation, SpawnParams);
		SpawnLocations.RemoveAt(Index, 1, true);
		for (int32 i = 0; i < SpawnLocations.Num() - 1; i++)
		{
			int32 firstIndex = FMath::RandRange(0, SpawnLocations.Num() - 1);
			int32 secondIndex = FMath::RandRange(0, SpawnLocations.Num() - 1);
			int32 spareIndex;
			if (firstIndex != secondIndex)
			{
				spareIndex = firstIndex;
				SpawnLocations[firstIndex] = SpawnLocations[secondIndex];
				SpawnLocations[secondIndex] = SpawnLocations[spareIndex];
			}


		}
		Index = FMath::RandRange(0, SpawnLocations.Num() - 1);
		FVector randChoice = SpawnLocations[Index];
		AMyCharacter* Character2 = World->SpawnActor<AMyCharacter>(PlayerBP, randChoice, spawnRotation, SpawnParams);

		UGameplayStatics::GetAllActorsOfClass(GetWorld(), PlayerBP, PlayerArray);
		int32 Itr2 = 0;

		for (int32 Itr = 0; Itr < PlayerArray.Num(); Itr++)
		{
			
			AMyCharacter* PlayerTest = Cast<AMyCharacter>(PlayerArray[Itr]);
			PlayerTest->PIndex = Itr;
			GEngine->AddOnScreenDebugMessage(-1, 5, FColor::Black, FString::FromInt(PlayerArray.Num()));
			UGameplayStatics::GetPlayerController(GetWorld(), PlayerTest->PIndex)->Possess(PlayerTest);
		}
	}
}

The Problem was when I called the create player function I had spawn pawn as false…