Okay, so!
I am currently learning UE5, and as a C++ programmer, I am only using C++.
After creating both the Character and the Controller, I checked with my senior that the code was right, yet, when I looked for this section
Yes, everything is there. I texted the code and imported it on my coworker’s PC and it works just fine, on mine, however, some things aren’t showing up
Apology for my late reply and thank you for your patience.
Okay, so: my task was to create basic WASD actions for my character using only the PlayerController.
First task was to create them with a basic Input, the deprecated system. Using that one, all commands work just fine. My Character can move without any problem. This is the main code in my Controller.
void AMyCharacterController::BeginPlay()
{
Super::BeginPlay();
// get the enhanced input subsystem
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
// add the mapping context so we get controls
Subsystem->AddMappingContext(InputMappingContext, 0);
UE_LOG(LogTemp, Warning, TEXT("BeginPlay"));
}
}
void AMyCharacterController::SetupInputComponent(UInputComponent* InputComponent)
{
Super::SetupInputComponent();
InputComponent->BindAxis("MoveForward", this, &AMyCharacterController::MoveForward);
}
void AMyCharacterController::MoveForward(float Value)
{
APawn* MyPawn = GetPawn();
if (MyPawn)
{
const FVector ForwardVector = GetControlRotation().Vector();
// Add movement in the forward direction
MyPawn->AddMovementInput(ForwardVector, Value);
}
}
Second task was to create all the commands all over again using the Enhanced Input System. Alas, I really can’t seem to understand the logic behind it to implement it the same way.
My Controller.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyCharacterController.h"
#include "Components/InputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "InputActionValue.h"
#include "Engine/LocalPlayer.h"
#include "EnhancedInputComponent.h"
void AMyCharacterController::BeginPlay()
{
Super::BeginPlay();
// get the enhanced input subsystem
if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(GetLocalPlayer()))
{
// add the mapping context so we get controls
Subsystem->AddMappingContext(InputMappingContext, 0);
UE_LOG(LogTemp, Warning, TEXT("BeginPlay"));
}
}
void AMyCharacterController::SetupInputComponent(UInputComponent* InputComponent)
{
// Bind functions to input
if (UEnhancedInputComponent* EnhancedInputComponent = CastChecked<UEnhancedInputComponent>(InputComponent))
{
EnhancedInputComponent->BindAction(MovementAction, ETriggerEvent::Started, this, &AMyCharacterController::MoveForward);
}
//InputComponent->BindAxis("MoveForward", this, &AMyCharacterController::MoveForward);
}
void AMyCharacterController::MoveForward(const FInputActionValue& Value)
{
const FVector2D MovementVector = Value.Get<FVector2D>();
const FVector Forward = GetActorForwardVector();
AddMovementInput(Forward, MovementVector.Y);
const FVector Right = GetActorForwardVector();
AddMovementInput(Right, MovementVector.X);
//APawn* MyPawn = GetPawn();
//if (MyPawn)
//{
// const FVector ForwardVector = GetControlRotation().Vector();
// // Add movement in the forward direction
// MyPawn->AddMovementInput(ForwardVector, Value);
//}
}
And My Controller.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MyCharacterController.generated.h"
class UInputMappingContext;
class UInputAction;
struct FInputActionValue;
/**
*
*/
UCLASS()
class MYPROJECT2_API AMyCharacterController : public APlayerController
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, Category = "EnhancedInput")
UInputMappingContext* InputMappingContext;
UPROPERTY(EditAnywhere, Category = "EnhancedInput", meta = (AllowPrivateAccess = "true"))
UInputAction* MovementAction;
protected:
/** Input Mapping Context to be used for player input */
virtual void SetupInputComponent(UInputComponent* InputComponent);
virtual void BeginPlay() override;
// Input functions
void MoveForward(const FInputActionValue& Value);
// End Actor interface
};
All the tutorial I followed don’t work . I am probably missing something, but I don’t understand what…
Why not just create a third person example with c++? It basically already has all of the needed functions with enhanced input (movement, camera movement) input mapping setup etc.
Okay, so.
The solution to this is easier than what I expected.
If I want to to the binding in my Custom Controller (which you migth want if you wish to use the controller for different things), first, in Begin Play, you must cast a LocalPlayer controller, check for the input system. If the Input System exists, add the mapping contest.
Next, do the binding in the SetupInputController (mind the override, be sure to put the Super).
Now, fo the part I was missing:
In the movement function, since you’re not in the Character class, you must tell the function to apply the movements to the current Pawn.