Hello, wonderful people.
Has someone tried to spawn a bot of different classes? I mean multiple classes in one experience.
I kind of did that, but then I found that they don’t have an ability system component, so I can’t apply gameplay effects to them.
I saw how people spawn different classes for players and bots, but it’s still one bot class for all bots. They just give different PawnData if a pawn is getting spawned for the AI controller. But I need to spawn squads of bots with different classes, strategy style.
code snippet
AActor* ULyraBotCreationComponent::SpawnBotOfClass(UClass* Controller, FVector Location, FRotator Rotation, UClass* PawnClass)
{
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
SpawnInfo.OverrideLevel = GetComponentLevel();
SpawnInfo.ObjectFlags |= RF_Transient;
FTransform Transform = FTransform(Rotation, Location);
AAIController* NewController = GetWorld()->SpawnActor<AAIController>(Controller, Transform, SpawnInfo);
//creating new pawn. Mimicing GameMode->RestartPlayer, because it can't use specific pawn class
APawn* NewPawn = nullptr;
UWorld* World = GetWorld();
NewPawn = World->SpawnActor<APawn>(PawnClass, Transform, SpawnInfo);
if (NewController != nullptr)
{
ALyraGameMode* GameMode = GetGameMode<ALyraGameMode>();
check(GameMode);
if (NewController->PlayerState != nullptr)
{
NewController->PlayerState->SetPlayerName(CreateBotName(NewController->PlayerState->GetPlayerId()));
}
GameMode->GenericPlayerInitialization(NewController);
//GameMode->RestartPlayer(NewController); //default implementation from SpawnOneBot, can't use pawn class, so replaced with hardcoded pawn spawn above.
NewController->SetPawn(NewPawn);
if (NewController->GetPawn() != nullptr)
{
if (ULyraPawnExtensionComponent* PawnExtComponent = NewController->GetPawn()->FindComponentByClass<ULyraPawnExtensionComponent>())
{
PawnExtComponent->CheckDefaultInitialization();
}
}
SpawnedBotList.Add(NewController);
}
return NewPawn;
}