Can't attach camera to capsule component

I’m working through this area of the tutorial:
https://docs.unrealengine.com/latest/INT/Programming/Tutorials/FirstPersonShooter/2/7/index.html

If I comment out this line:

FPSCameraComponent->SetupAttachment(GetCapsuleComponent());

It compiles and works fine.
However, if I don’t it throws the error below, which confuses me greatly since UCapsuleComponent obviously inherits USceneComponent.

Severity	Code	Description	Project	File	Line	Suppression State
Error	C2664	'void USceneComponent::SetupAttachment(USceneComponent *,FName)': cannot convert argument 1 from 'UCapsuleComponent *' to 'USceneComponent *'	Raid	C:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	16	

Please Help. I have no clue what I’m doing wrong here.

Full CPP, Headers, and Log. Thank you for your help.

FPSCharacter.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

UCLASS()
class RAID_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 DeltaSeconds) 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;
	
};

FPSCharacter.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "FPSCharacter.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 = true;

	// 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)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, 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);

	PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
	PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
	PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
	PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
	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;
}

Error Log

Severity	Code	Description	Project	File	Line	Suppression State
Error		Failed to produce item: C:\Software\Raid\Binaries\Win64\UE4Editor-Raid-6566.dll	Raid	C:\Software\Raid\Intermediate\ProjectFiles\ERROR	1	
Error (active)		cannot open source file "FPSCharacter.h"	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	3	
Error (active)		name followed by '::' must be a class or namespace name	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	7	
Error (active)		identifier "PrimaryActorTick" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	10	
Error (active)		identifier "FPSCameraComponent" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	13	
Error (active)		identifier "CreateDefaultSubobject" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	13	
Error (active)		identifier "UCameraComponent" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	13	
Error (active)		identifier "TEXT" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	13	
Error (active)		identifier "GetCapsuleComponent" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	16	
Error (active)		name followed by '::' must be a class or namespace name	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	26	
Error (active)		name followed by '::' must be a class or namespace name	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	28	
Error (active)		identifier "GEngine" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	31	
Error (active)		name followed by '::' must be a class or namespace name	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	33	
Error (active)		identifier "TEXT" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	33	
Error (active)		name followed by '::' must be a class or namespace name	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	39	
Error (active)		name followed by '::' must be a class or namespace name	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	41	
Error (active)		name followed by '::' must be a class or namespace name	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	46	
Error (active)		identifier "UInputComponent" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	46	
Error (active)		identifier "PlayerInputComponent" is undefined	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	46	
Error (active)		expected a ';'	Raid	c:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	47	
Error	C2664	'void USceneComponent::SetupAttachment(USceneComponent *,FName)': cannot convert argument 1 from 'UCapsuleComponent *' to 'USceneComponent *'	Raid	C:\Software\Raid\Source\Raid\Private\Characters\FPSCharacter.cpp	16	
Error	MSB3073	The command ""C:\Program Files\Epic Games\UE_4.16\Engine\Build\BatchFiles\Rebuild.bat" RaidEditor Win64 Development "C:\Software\Raid\Raid.uproject" -waitmutex" exited with code -1.	Raid	C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.MakeFile.Targets	42

With the new 4.16.1 Camera/CameraComponent.h needs to be included. My project is called “Raid” not “FPSProject” and I have tried including the Raid.h file with no avail. Same error.

try explicitly casting the type:

FPSCameraComponent->SetupAttachment(Cast<USceneComponent>(GetCapsuleComponent()));

if the cast fails, please try to #include both “USceneComponent.h” and “UCapsuleComponent.h”

does it work now?

1 Like

No luck :frowning: Thank you for the help. I’m going to probably try to figure out a new way to do the camera component by using another resource. It’s just too bad the official documentation tutorial doesn’t work :frowning: I’m not skilled enough yet to troubleshoot code within the engine to see why it’s not working. I do appreciate your effort though.

is the error same?
also just making sure, did you include both “USceneComponent.h” and “UCapsuleComponent.h”?
another thing to check - it is unlikely to be the cause of the trouble, but can you check if GetCapsuleComponent() is returning a null pointer?

Thanks for digging that out. Just a note Character.cpp and Character.h are reversed in the above code

,FPSCameraComponent->SetupAttachment((USceneComponent*)GetCapsuleComponent());

Cast like this :slight_smile:

Hi,

Add in .cpp :

#include “Camera/CameraComponent.h”

and Cast like this :

FPSCameraComponent->SetupAttachment((USceneComponent)GetCapsuleComponent());*

Good Luck :slight_smile:

Thank you for the help. I will try this tonight. I didn’t down vote by the way :slight_smile:

Thanks mate! Fixed <3

Hey, could you figure out the differences between the docs and the template version of the FirstPersonShooter to make it work for you? You can ask if something still went wrong, but if you do not have any more questions, make sure to check out your question for tracking reasons! <3

Substituting GetCapsuleComponent() with RootComponent did the trick for me.

I was following the same tutorial and I finally got it to work with the following:

In the FPSCharacter.h (or whatever you called it) make sure you have the following include:

#include “Runtime/Engine/Classes/Camera/CameraComponent.h”

In the FPSCharacter.cpp (or whatever you called it) make sure you have the following includes:

#include “Runtime/Engine/Classes/Components/CapsuleComponent.h”
#include “Runtime/Engine/Classes/Components/SceneComponent.h”

Just to post the rest of the cpp code from this portion of the tutorial:

// Create a first person camera component.
FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
// Attach the camera component to our capsule component.
FPSCameraComponent->SetupAttachment((USceneComponent*)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;

This post was able to answer some of the questions, but what I have written here got it to fully work. Thanks for all the previous answers :slight_smile:

All I needed to include was
#include “Components/CapsuleComponent.h”

Me too. Thanks

found the answers to this here Attach the camera component to our capsule component - C++ - Unreal Engine Forums

Including CameraComponent.h and CapsuleComponent.h in the cpp file fixes the problem without the need to resort to C-style casts, which can be dangerous.

#include "Camera/CameraComponent.h"
#include "Components/CapsuleComponent.h"

Thanks for your help,and I comfused with the same problem.I am always forget to include because of my History of C#