Need help debugging some team selection logic.

At least i assume that’s where the error is…

Basically i’m getting this error when i try to run the game from within the editor:

which breaks here (large image 2560*1440):

the error reads:



template< class T > T* CastChecked( UObject* Src, ECastCheckedType::Type CheckType = ECastCheckedType::NullChecked )
{
	CATCH_UNSUPPORTED_INTERFACECAST(T);
	
#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
	if( (!Src && CheckType == ECastCheckedType::NullChecked) || (Src && !Src->template IsA<T>() ) )
	{
		CastLogError(Src ? *Src->GetFullName() : TEXT("NULL"), *T::StaticClass()->GetName()); //<----- error on this line
	}
#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
	return (T*)Src;
}


But when i try to step into the code this happens:

I don’t feel brave enough to try and do everything through the full source code yet. But this apparently means i can’t debug some errors properly, so i was wondering if anybody might be able to see the issue?
Here’s the team selection logic:



void ASmashyGameMode::PostLogin(APlayerController* NewPlayer)
{
	//place player on a team before Super (VoIP team based init, findplayerstart, etc.)
	ASmashyPlayerState* NewPlayerState = CastChecked<ASmashyPlayerState>(NewPlayer->PlayerState);
	const int32 TeamNum = ChooseTeam(NewPlayerState);
	NewPlayerState->SetTeamNum(TeamNum);

	Super::PostLogin(NewPlayer);

	//Update spectator location for client
	ASmashyPlayerController* NewPC = Cast<ASmashyPlayerController>(NewPlayer);
	if (NewPC && NewPC->GetPawn() == NULL)
	{
		NewPC->ClientSetSpectatorCamera(NewPC->GetSpawnLocation(), NewPC->GetControlRotation());
	}

	//Notify new player if match is already in progress
	if (NewPC && IsMatchInProgress())
	{
		NewPC->ClientGameStarted();
		NewPC->ClientStartOnlineGame();
	}
}

int32 ASmashyGameMode::ChooseTeam(ASmashyPlayerState* ForPlayerState) const
{
	//create an array of teams
	TArray<int32> TeamBalance;
	TeamBalance.AddZeroed(NumTeams);

	//get current team balance
	for (int32 i = 0; i < GameState->PlayerArray.Num(); i++)
	{
		ASmashyPlayerState const* const TestPlayerState = Cast<ASmashyPlayerState>(GameState->PlayerArray*);
		if (TestPlayerState && TestPlayerState != ForPlayerState && TeamBalance.IsValidIndex(TestPlayerState->GetTeamNum()))
		{
			TeamBalance[TestPlayerState->GetTeamNum()]++;
		}
	}

	//find least populated team
	int32 BestTeamScore = TeamBalance[0];
	for (int32 i = 1; i < TeamBalance.Num(); i++)
	{
		if (BestTeamScore > TeamBalance*)
		{
			BestTeamScore = TeamBalance*;
		}
	}

	//there could be more than one (useful if > 2 teams in match/game)
	//also if both teams are even
	TArray<int32> BestTeams;
	for (int32 i = 0; i < TeamBalance.Num(); i++)
	{
		if (TeamBalance* == BestTeamScore)
		{
			BestTeams.Add(i);
		}
	}

	//get random number from best list
	const int32 RandomBestTeam = BestTeams[FMath::RandHelper(BestTeams.Num())];
	return RandomBestTeam;
}


I am by no means a expert, but i am assuming it is dieing at ASmashyPlayerState* NewPlayerState = CastChecked<ASmashyPlayerState>(NewPlayer->PlayerState); in postlogin, it seems that its hitting a nullpointer with the playerstate, or at least that is my guess as i have not looked to much into when playerstate is created, is playerstate actually built at this point?, i am sorry i cant be much more help, but maby it will point you in the correct direction.

Can you check your call stack to see which line of your code is being called at the time the exception is thrown?

Sorry - I misread, you only have one call to CastChecked. It looks like you are casting a concrete type when it’s looking for an interface. Just at a guess. Maybe Simply calling Cast will work.

So in order to “fix” this…

i copied my /source/ folder to a different location. deleted the entire project, started it again (with the same name) copied the old /source/ folder back, right clicked Smashy.uproject -> Generate visual studio project files. and now everything works as it should do. no weird breaks or anything.

the reason i opted to do this in the end, was because after making sure that my gamemode was selected properly in world settings, and putting a break point in the constructor, that break point was never hit. not even after rebooting, cleaning the solution, and rebuilding from scratch.