BeginPlay is called when the player begin play (when connected to server and start playing)
Also looks like shooterGame sometimes crash:
UE_LOG(LogShooter, Log, TEXT("InitBot: %s spawned on %s"), *Bot->GetName(), *BestStart->GetName());
sometimes (every 50 tests for example) GetName crash trying to read Name field (null i think), i changed a bit the code and hoping dont crash anymore, my working code is…
ShooterGame_TeamDeathMatch.cpp
void AShooterGame_TeamDeathMatch::InitBot(AShooterBot* Bot, int BotNumber)
{
// Setup bot name / teleport to spawn position
Super::InitBot(Bot, BotNumber);
}
ShooterGameMode.cpp
void AShooterGameMode::InitBot(AShooterBot* Bot, int BotNumber)
{
AShooterAIController* AIPC = Bot ? Cast<AShooterAIController>(Bot->GetController()) : NULL;
if (AIPC)
{
AShooterPlayerState* BotPlayerState = CastChecked<AShooterPlayerState>(AIPC->PlayerState);
if (BotPlayerState)
{
AActor* BestStart = ChoosePlayerStart(AIPC);
// set bot name
FString BotName = FString::Printf(TEXT("Bot %d"), BotNumber);
BotPlayerState->PlayerName = BotName;
// set team number
const int32 TeamNum =/* ChooseTeam(BotPlayerState)*/1;
BotPlayerState->SetTeamNum(TeamNum);
// setup initial spawn position
Bot->TeleportTo(BestStart->GetActorLocation(), BestStart->GetActorRotation(), false, true);
// setup stuff
Bot->SetupCharacterStuff();
UE_LOG(LogShooter, Log, TEXT("InitBot: %s spawned on %s"), *Bot->GetName(), *BestStart->GetName());
}
else
{
UE_LOG(LogShooter, Error, TEXT("InitBot: PlayerState-NULL"));
}
}
}
ShooterCharacter.cpp
void AShooterCharacter::PossessedBy(class AController* InController)
{
Super::PossessedBy(InController);
// [server] as soon as PlayerState is assigned, set team colors of this pawn for local player
UpdateTeamColorsAllMIDs();
SetupCharacterStuff();
}
void AShooterCharacter::SetupCharacterStuff()
{
if (PlayerState != NULL)
{
AShooterPlayerState* MyPlayerState = Cast<AShooterPlayerState>(PlayerState);
if (MyPlayerState != NULL)
{
TeamNum = MyPlayerState->GetTeamNum();
if (TeamNum != -1)
{
Health = GetMaxHealth();
if (TeamNum == 0)
{
//...
}
else
{
//..
}
}
}
}
}
Looks like all is working fine for now, i hope the crash is fixed too.