UE4.24.2 C++ FPS tutorial has errors in code

Hello,

I am following this tutorial : [https://docs.unrealengine.com/en-US/Programming/Tutorials/FirstPersonShooter/2/7/index.html][1]
but I have a few problems.

1st one : my code (until the step 2.7 of the tutorial) compiles and works perfectly, but Visual Studio (2017) spotted 23 errors. I think it is because it does not recognize UE4’s syntax. Is it possible to solve this?

Here is the code of the .cpp file :

// Copyright 2019-2019 ENGEMANN Damien, All Rights Reserved.


#include "FPSCharacter.h"
#include "Fracture.h"

// Sets default values
AFPSCharacter::AFPSCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = false;

	/*
	// Create a first person camera component.
	FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
	// Attach the camera component to our capsule component.
	FPSCameraComponent->SetupAttachment(GetCapsuleComponent());
	// Position the camera slightly above the eyes.
	FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));
	// Allow the pawn to control camera rotation.
	FPSCameraComponent->bUsePawnControlRotation = true;
	*/
}

// Called when the game starts or when spawned
void AFPSCharacter::BeginPlay()
{
	Super::BeginPlay();

	if (GEngine)
	{
		// Put up a debug message for five seconds. The -1 "Key" value (first argument) indicates that we will never need to update or refresh this message.
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
	}
	
}

// Called every frame
void AFPSCharacter::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

// Called to bind functionality to input
void AFPSCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	// Set up "movement" bindings.
	PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);

	// Set up "look" bindings.
	PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
	PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);

	// Set up "action" bindings.
	PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
	PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
}

void AFPSCharacter::MoveForward(float Value)
{
	// Find out which way is "forward" and record that the player wants to move that way.
	FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
	AddMovementInput(Direction, Value);
}

void AFPSCharacter::MoveRight(float Value)
{
	// Find out which way is "right" and record that the player wants to move that way.
	FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
	AddMovementInput(Direction, Value);
}

void AFPSCharacter::StartJump()
{
	bPressedJump = true;
}


void AFPSCharacter::StopJump()
{
	bPressedJump = false;
}

And here is the code of the .h :

// Copyright 2019-2019 ENGEMANN Damien, All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "FPSCharacter.generated.h"

UCLASS()
class FRACTURE_API AFPSCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	AFPSCharacter();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	// Handles input for moving forward and backward.
	UFUNCTION()
	void MoveForward(float Value);

	// Handles input for moving right and left.
	UFUNCTION()
	void MoveRight(float Value);

	// Sets jump flag when key is pressed.
	UFUNCTION()
	void StartJump();

	// Clears jump flag when key is released.
	UFUNCTION()
	void StopJump();

	/*
	// FPS camera.
	UPROPERTY(VisibleAnywhere)
	UCameraComponent* FPSCameraComponent;
	*/
};

2nd problem, the one that blocks me from continuing this tutorial :

When following the steps of this specific page : [https://docs.unrealengine.com/en-US/Programming/Tutorials/FirstPersonShooter/2/7/index.html][3] , the code does not compile anymore, and I don’t know where the errors are.

Note that I did not follow exactly the tutorial, because I named my project “Fracture” instead of “FPSProject”.
Also, I am using the Github’s UE4 instead of the launcher version, because I am using Chaos. I manually compiled the engine and activated Chaos by following these steps : [Chaos Destruction Overview | Unreal Engine Documentation][5]

I forgot to add a 3rd bug that I don’t know how to solve :

When looking up or down with the camera (pitch axis), the forward/backward speed is beeing reduced.

void AFPSCharacter::MoveForward(float Value)
{
	// Find out which way is "forward" and record that the player wants to move that way.
	FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
	AddMovementInput(Direction, Value);
}

void AFPSCharacter::MoveRight(float Value)
{
	// Find out which way is "right" and record that the player wants to move that way.
	FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
	AddMovementInput(Direction, Value);
}

It seems the code only takes care of the X axis, where the camera is pointing horizontally, to define where forward is? I don’t see any mistake in this code?

I found part of the solution for the problem #2 :

I added #include "Camera/CameraComponent.h" to the FPSCharacter.h file.

Now, the compile still fails but there is only 1 error :

C:\UE4_forks\Unreal_Projects\Fracture\Source\Fracture\FPSCharacter.cpp(20): note: Les types point?s n'ont aucun rapport entre eux?; conversion n?cessitant reinterpret_cast, cast de style C ou cast de style fonction

C:\UE4_forks\Unreal_Projects\Fracture\Source\Fracture\FPSCharacter.cpp(20) : error C2664: 'void USceneComponent::SetupAttachment(USceneComponent *,FName)'?: impossible de convertir l'argument 1 de 'UCapsuleComponent *' en 'USceneComponent *'

(in english, the error says : error C2664: ‘void USceneComponent::SetupAttachment(USceneComponent *,FName)’?: impossible to convert the argument 1 from ‘UCapsuleComponent *’ to ‘USceneComponent *’)

I finally resolved the problem #2, after diving during hours in forums.
I simply replaced

FPSCameraComponent->SetupAttachment((GetCapsuleComponent());

by

FPSCameraComponent->SetupAttachment(RootComponent);

■■■■ the UE4 documentation needs some rework!

So I still need to fix the problem #3. #1 and #2 are solved.

Well, I solved the problem #3 too now!

I found this info on a forum :

*The reason you move slowest when you
are not looking straight ahead is
because those vectors are wasting some
of their length going upward/downwards
which results in less length to go
forward, hence slower speeds. You need
to zero your Z axis on your direction
vector:

Code:*

voidAFPSController::MoveForward(float
AxisValue) {
     //Get the forward vector
    FVector direction = FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::X);
 
    // We get the flattened forward look direction and normalize it
    direction = FVector(direction.X, direction.Y, 0.f).GetSafeNormal();    
    AddMovementInput(direction, AxisValue);
}

This gives the direction
your player is looking on a lateral
plane.

So now my correct FPSCharacter.ccp file for UE 4.24.2 contains :

void AFPSCharacter::MoveForward(float Value)
{
	// Find out which way is "forward" and record that the player wants to move that way.
	FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
	//FVector Direction = FRotationMatrix(GetControlRotation()).GetScaledAxis(EAxis::X);	//This one works too
	Direction = FVector(Direction.X, Direction.Y, 0.f).GetSafeNormal();
	AddMovementInput(Direction, Value);

	//debug vitesse :
	FString vitesse = AFPSCharacter::GetVelocity().ToString();
	GEngine->AddOnScreenDebugMessage(1, 0.0f, FColor::Purple, "Velocity =" + vitesse);
}

The problems are not finished yet actually…
Now this code literally does nothing :

// Position the camera slightly above the eyes.
FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));

With or without it it’s the same, and changing the 50.0f value or removing + BaseEyeHeight does not change the camera position at all…

Maybe it’s because I’ve set
// Attach the camera component to our capsule component. FPSCameraComponent->SetupAttachment(RootComponent);

instead of

// Attach the camera component to our capsule component.
FPSCameraComponent->SetupAttachment(GetCapsuleComponent());

So I’ve used the lower version of this code, the trick to solve it was to #include "Components/CapsuleComponent.h"

That’s weird. According to https://docs.unrealengine.com/en-US/API/Runtime/Engine/GameFramework/ACharacter/GetCapsuleComponent/index.html, it should’ve worked only with #include "GameFramework/Character.h"