Guest User

Tempalte Platformer

a guest
Jan 19th, 2015
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. void AGeneralGameMode::LoadPlayers()
  2. {
  3.     // There is no point in running this loop if MaxPlayers is not set
  4.     if (GetMaxPlayerCount())
  5.     {
  6.         UWorld* currentWorld = GetWorld();
  7.  
  8.         for (int32 a = 1; a <= GetMaxPlayerCount(); a++)
  9.         {
  10.             // Create a PlayerCharacter
  11.             ABasePlayer* newPawn = ABasePlayer::Instantiate(currentWorld);
  12.  
  13.             // Find our player starting position
  14.             FString actorName = FString::Printf(TEXT("Player%dStart"), a);
  15.             APlayerStart* playerStart = ActorHelper::FindActorsByTypeAndName<APlayerStart>(currentWorld, actorName);
  16.  
  17.             // Go and create local player
  18.             UGameInstance* gameInstance = GetGameInstance();
  19.             currentWorld->GetGameViewport()->SetDisableSplitscreenOverride(true);
  20.             FString error;
  21.             ULocalPlayer* newLocalPlayer = gameInstance->CreateLocalPlayer(a, error, true);    
  22.  
  23.             // If we found a player starting position
  24.             if (playerStart != NULL)
  25.             {
  26.                 // Pass player starting position into PlayerCharacter and move to this position
  27.                 SetupPlayerModel* setupPlayerModel = new SetupPlayerModel();
  28.                 setupPlayerModel->PlayerStart = playerStart;
  29.                 setupPlayerModel->PlayerNumber = a;
  30.                 newPawn->Setup(setupPlayerModel);
  31.  
  32.                 // Go and get the PlayerController that was created by CreateLocalPlayer
  33.                 APlayerController* playerController = UGameplayStatics::GetPlayerController(currentWorld, a);
  34.  
  35.                 // Set the player to our Local Player
  36.                 playerController->SetPlayer(newLocalPlayer);
  37.                 // Take over the PlayerCharacter we instantiated.
  38.                 playerController->Possess(newPawn);
  39.  
  40.                 // Place our PlayerCharacter into an array for future handle
  41.                 Players.Add(newPawn);
  42.             }
  43.             else
  44.             {
  45.                 throw FString::Printf(TEXT("You are searching for a Player%dStart but it could not be found"), a);
  46.             }
  47.         }
  48.     }
  49.     else
  50.     {
  51.         throw "MaxPlayers was not set";
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment