// Copyright Epic Games, Inc. All Rights Reserved. #include "SideScrollingPlayerController.h" #include "EnhancedInputSubsystems.h" #include "InputMappingContext.h" #include "Kismet/GameplayStatics.h" #include "GameFramework/PlayerStart.h" #include "SideScrollingCharacter.h" #include "Engine/LocalPlayer.h" #include "Engine/World.h" #include "Blueprint/UserWidget.h" #include "MyProject.h" #include "Widgets/Input/SVirtualJoystick.h" void ASideScrollingPlayerController::BeginPlay() { Super::BeginPlay(); // only spawn touch controls on local player controllers if (ShouldUseTouchControls() && IsLocalPlayerController()) { // spawn the mobile controls widget MobileControlsWidget = CreateWidget(this, MobileControlsWidgetClass); if (MobileControlsWidget) { // add the controls to the player screen MobileControlsWidget->AddToPlayerScreen(0); } else { UE_LOG(LogMyProject, Error, TEXT("Could not spawn mobile controls widget.")); } } } void ASideScrollingPlayerController::SetupInputComponent() { Super::SetupInputComponent(); // only add IMCs for local player controllers if (IsLocalPlayerController()) { // add the input mapping context if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem(GetLocalPlayer())) { for (UInputMappingContext* CurrentContext : DefaultMappingContexts) { Subsystem->AddMappingContext(CurrentContext, 0); } // only add these IMCs if we're not using mobile touch input if (!ShouldUseTouchControls()) { for (UInputMappingContext* CurrentContext : MobileExcludedMappingContexts) { Subsystem->AddMappingContext(CurrentContext, 0); } } } } } void ASideScrollingPlayerController::OnPossess(APawn* InPawn) { Super::OnPossess(InPawn); // subscribe to the pawn's OnDestroyed delegate InPawn->OnDestroyed.AddDynamic(this, &ASideScrollingPlayerController::OnPawnDestroyed); } void ASideScrollingPlayerController::OnPawnDestroyed(AActor* DestroyedActor) { // find the player start TArray ActorList; UGameplayStatics::GetAllActorsOfClass(GetWorld(), APlayerStart::StaticClass(), ActorList); if (ActorList.Num() > 0) { // spawn a character at the player start const FTransform SpawnTransform = ActorList[0]->GetActorTransform(); if (ASideScrollingCharacter* RespawnedCharacter = GetWorld()->SpawnActor(CharacterClass, SpawnTransform)) { // possess the character Possess(RespawnedCharacter); } } } bool ASideScrollingPlayerController::ShouldUseTouchControls() const { // are we on a mobile platform? Should we force touch? return SVirtualJoystick::ShouldDisplayTouchInterface() || bForceTouchControls; }