I am trying to make a local co-op game… My goal here is to have more than 1 player using different controllers… or different keys on the keyboard.
I have the following Load Players function:
void AGeneralGameMode::LoadPlayers()
{
// There is no point in running this loop if MaxPlayers is not set
if (GetMaxPlayerCount())
{
for (int32 a = 1; a <= GetMaxPlayerCount(); a++)
{
UWorld* currentWorld = GetWorld();
ABasePlayer* newPlayer = ABasePlayer::Instantiate(currentWorld);
FString actorName = FString::Printf(TEXT("Player%dStart"), a);
APlayerStart* playerStart = ActorHelper::FindActorsByTypeAndName<APlayerStart>(currentWorld, actorName);
if (playerStart != NULL)
{
SetupPlayerModel* setupPlayerModel = new SetupPlayerModel();
setupPlayerModel->PlayerStart = playerStart;
setupPlayerModel->PlayerNumber = a;
newPlayer->Setup(setupPlayerModel);
ABasePlayerController* newController = currentWorld->SpawnActor<ABasePlayerController>(ABasePlayerController::StaticClass());
if (newController)
{
currentWorld->AddController(newController);
newController->Possess(newPlayer);
newController->GetPawn()->Restart();
}
Players.Add(newPlayer);
}
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";
}
}
- I first get the world
- Then i create a new ABasePlayer (which is APlayerCharacter)
- Then i go find the APlayerStart actors in my map
- Then i place the player start into a SetupPlayerModel
- Then i create a new ABasePlayerController which is APlayerController
- Finally I AddController to the current world, Possess the player and restart the player’s pawn.
Here is my ABasePlayer:
void ABasePlayer::Setup(SetupPlayerModel* setupPlayerModel)
{
this->setupPlayerModel = setupPlayerModel;
// Set up player starting position
FVector location = setupPlayerModel->PlayerStart->GetActorLocation();
SetActorLocation(location);
if (GEngine)
{
//Debug line shows up
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::SanitizeFloat(GetActorRotation().Yaw));
}
}
void ABasePlayer::Init()
{
Score = new BaseScore();
}
bool ABasePlayer::IsActive()
{
return Active;
}
void ABasePlayer::SetAsActive()
{
Active = true;
}
void ABasePlayer::Deactivate()
{
Active = false;
}
BaseScore& ABasePlayer::GetScore()
{
return *Score;
}
void ABasePlayer::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
// set up gameplay key bindings
InputComponent->BindAxis(*FString::Printf(TEXT("Player%dMoveRight"), setupPlayerModel->PlayerNumber), this, &ABasePlayer::MoveRight);
}
It inherits from the default APlayerCharacter you get when you first create a project… If you must see it, it looks like this: http://pastebin.com/3rWFgg5A
Finally, my input mappings look like this:
[/Script/Engine.InputSettings]
DefaultTouchInterface=/Engine/MobileResources/HUD/LeftVirtualJoystickOnly.LeftVirtualJoystickOnly
+ActionMappings=(ActionName="Jump", Key=W)
+ActionMappings=(ActionName="Jump", Key=Up)
+ActionMappings=(ActionName="Jump", Key=SpaceBar)
+ActionMappings=(ActionName="Jump", Key=Gamepad_FaceButton_Bottom)
+AxisMappings=(AxisName="MoveRight", Key=A, Scale=-1.f)
+AxisMappings=(AxisName="MoveRight", Key=D, Scale=1.f)
+AxisMappings=(AxisName="MoveRight", Key=Gamepad_LeftX, Scale=1.f)
+AxisMappings=(AxisName="Player1MoveRight", Key=A, Scale=-1.f)
+AxisMappings=(AxisName="Player1MoveRight", Key=D, Scale=1.f)
+AxisMappings=(AxisName="Player1MoveRight", Key=Gamepad_LeftX, Scale=1.f)
+AxisMappings=(AxisName="Player2MoveRight", Key=J, Scale=-1.f)
+AxisMappings=(AxisName="Player2MoveRight", Key=L, Scale=1.f)
+AxisMappings=(AxisName="Player2MoveRight", Key=Gamepad_LeftX, Scale=1.f)
I cannot understand why my players do not react to the keyboard…? Any ideas?
P.S GetMaxPlayerCount() returns 2.