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"

When I try to access the camera, it is null

I am trying to just shoot a ray from the camera that the user is using. It is telling me that the camera is a null pointer. // Fill out your copyright notice in the Description page of Project Settings.

 #include "MyCharacter.h"
 
 
 
 // Sets default values
 AMyCharacter::AMyCharacter()
 {
     ThirdPerson = 2;
     FirstPerson = 1;
     // 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.
     CameraSpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraSpringArm"));
     CharacterCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
     // Attach the camera component to our capsule component.
     CameraSpringArm->SetupAttachment(RootComponent);
     CharacterCamera->SetupAttachment(CameraSpringArm);
     // Allow the pawn to control camera rotation.
     CharacterCamera->bUsePawnControlRotation = true;
     SwitchCamera(CharacterCamera, CameraSpringArm, ThirdPerson);
     bPressedCameraSwap = false;
     bFire = false;
 }
 
 // Called when the game starts or when spawned
 void AMyCharacter::BeginPlay()
 {
     Super::BeginPlay();
 
     if (GEngine)
     {
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, TEXT("We are using MyCharacter!"));
     }
 }
 
 // Called every frame
 void AMyCharacter::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
 
 }
 
 // Called to bind functionality to input
 void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
 {
     Super::SetupPlayerInputComponent(PlayerInputComponent);
     
     PlayerInputComponent->BindAxis("MoveForward", this, &AMyCharacter::MoveForward);
     PlayerInputComponent->BindAxis("MoveSide", this, &AMyCharacter::MoveSide);
     PlayerInputComponent->BindAxis("TurnUp", this, &AMyCharacter::AddControllerYawInput);
     PlayerInputComponent->BindAxis("TurnSide", this, &AMyCharacter::AddControllerPitchInput);
     PlayerInputComponent->BindAction("CameraSwap", IE_Pressed, this, &AMyCharacter::CameraSwap);
     PlayerInputComponent->BindAction("CameraSwap", IE_Released, this, &AMyCharacter::CameraSwapStop);
     PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &AMyCharacter::Fire);
 }
 
 void AMyCharacter::MoveForward(float Value)
 {
     if (Value != 0.0f)
     {
         AddMovementInput(GetActorForwardVector(), Value);
     }
 }
 
 void AMyCharacter::MoveSide(float Value)
 {
     if (Value != 0.0f)
     {    
         AddMovementInput(GetActorRightVector(), Value);
     }
 }
 
 void AMyCharacter::CameraSwap()
 {
     bPressedCameraSwap = true;
 
     if (bThirdPersonEnabled)
     {
         SwitchCamera(CharacterCamera, CameraSpringArm, ThirdPerson);
         bFirstPersonEnabled = true;
         bThirdPersonEnabled = false;
     }
     else
     {
         bFirstPersonEnabled = false;
         SwitchCamera(CharacterCamera, CameraSpringArm, FirstPerson);
         bThirdPersonEnabled = true;
     }
 }
 
 void AMyCharacter::CameraSwapStop()
 {
     bPressedCameraSwap = false;
 }
 
 void AMyCharacter::Fire()
 {
     bFire = true;
     float Distance = 1000.0f;
     FHitResult HitResult;
     FVector StartTrace = CharacterCamera->GetComponentLocation(); //Its breaking here
     FVector ForwardVector = CharacterCamera->GetForwardVector();
     FVector EndTrace = ((ForwardVector * Distance) + StartTrace);
     FCollisionQueryParams TraceParams;
 
     GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace, ECC_Visibility, TraceParams);
     DrawDebugLine(GetWorld(), StartTrace, EndTrace,FColor(255,0,0), true);
     
 }
 

Here is the header file.

 #pragma once
 
 #include "CoreMinimal.h"
 #include "EngineGlobals.h"
 #include "Engine/Engine.h"
 #include "Components/InputComponent.h"
 #include "GameFramework/CharacterMovementComponent.h"
 #include "Camera/CameraComponent.h"
 #include "GameFramework/Controller.h"
 #include "GameFramework/Character.h"
 #include "GameFramework/SpringArmComponent.h"
 
 #include "MyCharacter.generated.h"
 
 
 UCLASS()
 class LEARNING_API AMyCharacter : public ACharacter
 {
     GENERATED_BODY()
         
 public:
     // Sets default values for this character's properties
     AMyCharacter();
 
 protected:
     // Called when the game starts or when spawned
     void BeginPlay() override;
     
     
 public:    
     // Called every frame
     void Tick(float DeltaTime) override;
 
     // Called to bind functionality to input
     void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
 
 protected:
     UFUNCTION()
         void MoveForward(float Val);
     //handles strafing
     UFUNCTION()
         void MoveSide(float Val);
     //Handles swaping camera on press
     UFUNCTION()
         void CameraSwap();
     //Clears swaping camera Flag
     UFUNCTION()
         void CameraSwapStop();
     UFUNCTION()
         void Fire();
     UFUNCTION()
         void FireStop();
     
     
 
 public:
     //If False there is an error. If true it was successful
     bool SwitchCamera(UCameraComponent* Camera, USpringArmComponent* SpringArm, int CameraType);
     //Set Spring Arm properties when changing views
     bool SetSpringArmProp(USpringArmComponent* SpringArm, int View);
 
     UCameraComponent* GetCharacterCamera() const { return CharacterCamera; }
     UPROPERTY(VisibleAnywhere)
         UCameraComponent* CharacterCamera;
     UPROPERTY(VisibleAnywhere)
         USpringArmComponent* CameraSpringArm;
 
     UPROPERTY()
         bool bPressedCameraSwap;
     UPROPERTY()
         bool bFirstPersonEnabled;
     UPROPERTY()
         bool bThirdPersonEnabled;
     UPROPERTY()
         bool bFire;
     
     
     
     //A camera type used for SwitchCamera
     int ThirdPerson;
     //A camera type used for SwitchCamera
     int FirstPerson;
     
 };

Product Version: UE 4.21
Tags:
more ▼

asked Feb 07 '19 at 11:36 PM in C++ Programming

avatar image

Jams Charles
8 1 1

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

1 answer: sort voted first

Not sure if it was a bug but i fixed it by renaming the variable charactercamera. I think it broke after i messed with the TEXT in the createdefaultsubobject function.

more ▼

answered Feb 08 '19 at 10:21 PM

avatar image

Jams Charles
8 1 1

(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