Hi all,
I’m trying to make APlayerController handle the input for ACharacter, although I have a problem where ACharacter is linked to APlayerController but not the other way around nor both accessing each other.
I’m trying to make my code more modular, and would lake to create different controllers for a particular character e.g. controller, mouse, keyboard .etc
Here’s what I have so far… And ideas what the problem is or any tips, I’m a beginner to the framework although am quite fluent in C++ with a dislike for blueprints (So please don’t suggest any thank you).
The Game Mode:
#include "TheGameCharacterTest.h"
#include "TheGameCharacterTestGameMode.h"
#include "CharacterClasses/MainCharacter.h"
#include "CharacterControllers/MainCharacterKeyboard.h"
ATheGameCharacterTestGameMode::ATheGameCharacterTestGameMode()
{
DefaultPawnClass = AMainCharacter::StaticClass();
PlayerControllerClass = AMainCharacterKeyboard::StaticClass();
}
The Character:
#include "TheGameCharacterTest.h"
#include "MainCharacter.h"
// Sets default values
AMainCharacter::AMainCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
AMainCharacter::AttachCamera();
AutoPossessPlayer = EAutoReceiveInput::Player0;
}
void AMainCharacter::AttachCamera()
{
// Create a CameraComponent and arm
GetCapsuleComponent()->SetWorldLocation(FVector(0.0f, 0.0f, 0.0f));
CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
CameraSpringArm->AttachParent = GetCapsuleComponent();
CameraSpringArm->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
CameraSpringArm->SetRelativeRotation(FRotator(0.0f, 0.0f, 0.0f));
CameraSpringArm->TargetArmLength = 0.0f;
CameraSpringArm->bEnableCameraLag = true;
CameraSpringArm->CameraLagMaxDistance = 10.0f;
CameraSpringArm->CameraLagSpeed = 5.0f;
FirstPersonCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
FirstPersonCameraComponent->AttachTo(CameraSpringArm, USpringArmComponent::SocketName);
FirstPersonCameraComponent->RelativeLocation = FVector(0.0f, 0.0f, 64.f); // Position the camera
FirstPersonCameraComponent->RelativeRotation = FRotator(0.0f, 0.0f, 0.0f);
}
// Called when the game starts or when spawned
void AMainCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AMainCharacter::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
// Called to bind functionality to input
//void AMainCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)
//{
// Super::SetupPlayerInputComponent(InputComponent);
//}
The Player keyboard controller
#include "TheGameCharacterTest.h"
#include "MainCharacterKeyboard.h"
#include "Engine.h"
AMainCharacterKeyboard::AMainCharacterKeyboard()
{
Pawn = GetPawn();
if (Pawn != NULL) GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::Printf(TEXT("Pawn")));
else GEngine->AddOnScreenDebugMessage(-1, 1.0f, FColor::Green, FString::Printf(TEXT("No Pawn")));
this->Possess(Pawn);
};
void AMainCharacterKeyboard::SetupInputComponent()
{
Super::SetupInputComponent();
static const FName InputComponentName(TEXT("TravelInputComponent0"));
InputComponent = ConstructObject<UInputComponent>(UInputComponent::StaticClass(), this, InputComponentName);
check(InputComponent);
if (InputComponent != NULL)
{
InputComponent->BindAxis("MoveForward", this, &AMainCharacterKeyboard::MoveForward);
}
};
void AMainCharacterKeyboard::MoveForward(float Value)
{
if (Value != 0.0f && (Pawn != NULL))
{
GEngine->AddOnScreenDebugMessage(-1, 0.005f, FColor::Green, FString::Printf(TEXT("Moveing %f"), Value));
Pawn->AddMovementInput(Pawn->GetActorForwardVector(), Value);
//FRotator const ControlSpaceRot = GetControlRotation();
// transform to world space and add it
//GetPawn()->AddMovementInput(FRotationMatrix(ControlSpaceRot).GetScaledAxis(EAxis::Y), Value);
}
};