x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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    
 





Product Version: UE 4.16
Tags:
more ▼

asked Jun 17 '17 at 12:58 PM in C++ Programming

avatar image

FatBalloon
20 2 4 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

7 answers: sort voted first

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

more ▼

answered Jul 01 '17 at 12:01 PM

avatar image

VinQbator
56 4 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hi,

Add in .cpp :

#include "Camera/CameraComponent.h"

and Cast like this :

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

Good Luck :-)

more ▼

answered Jul 13 '17 at 02:38 PM

avatar image

poseidonsr
56 1 3 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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?

more ▼

answered Jun 17 '17 at 04:09 PM

avatar image

Arty-McLabin
868 19 41 61

avatar image FatBalloon Jun 18 '17 at 08:02 PM

No luck :( 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 :( 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.

avatar image Arty-McLabin Jun 18 '17 at 09:41 PM

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?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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 :)

more ▼

answered Oct 02 '17 at 05:55 PM

avatar image

Devin Sherry
11 1

avatar image MoFried Feb 10 '18 at 07:19 PM

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

avatar image David Haskell May 04 '18 at 11:17 PM

Me too. Thanks

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

Cast like this :-)

more ▼

answered Jul 13 '17 at 06:44 PM

avatar image

poseidonsr
56 1 3 3

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
more ▼

answered Feb 02 '19 at 05:24 AM

avatar image

Imagine-Games
214 5 10 14

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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"
 
more ▼

answered May 05 '19 at 06:16 PM

avatar image

VolatileAgent
51 2 6 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question