Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void AGeneralGameMode::LoadPlayers()
- {
- // There is no point in running this loop if MaxPlayers is not set
- if (GetMaxPlayerCount())
- {
- UWorld* currentWorld = GetWorld();
- for (int32 a = 1; a <= GetMaxPlayerCount(); a++)
- {
- // Create a PlayerCharacter
- ABasePlayer* newPawn = ABasePlayer::Instantiate(currentWorld);
- // Find our player starting position
- FString actorName = FString::Printf(TEXT("Player%dStart"), a);
- APlayerStart* playerStart = ActorHelper::FindActorsByTypeAndName<APlayerStart>(currentWorld, actorName);
- // Go and create local player
- UGameInstance* gameInstance = GetGameInstance();
- currentWorld->GetGameViewport()->SetDisableSplitscreenOverride(true);
- FString error;
- ULocalPlayer* newLocalPlayer = gameInstance->CreateLocalPlayer(a, error, true);
- // If we found a player starting position
- if (playerStart != NULL)
- {
- // Pass player starting position into PlayerCharacter and move to this position
- SetupPlayerModel* setupPlayerModel = new SetupPlayerModel();
- setupPlayerModel->PlayerStart = playerStart;
- setupPlayerModel->PlayerNumber = a;
- newPawn->Setup(setupPlayerModel);
- // Go and get the PlayerController that was created by CreateLocalPlayer
- APlayerController* playerController = UGameplayStatics::GetPlayerController(currentWorld, a);
- // Set the player to our Local Player
- playerController->SetPlayer(newLocalPlayer);
- // Take over the PlayerCharacter we instantiated.
- playerController->Possess(newPawn);
- // Place our PlayerCharacter into an array for future handle
- Players.Add(newPawn);
- }
- else
- {
- throw FString::Printf(TEXT("You are searching for a Player%dStart but it could not be found"), a);
- }
- }
- }
- else
- {
- throw "MaxPlayers was not set";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment