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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

GetPawn() in a PlayerController class returns NULL

Hello! I have been trying to make a custom PlayerController class, I guess it is almost done, but I have some issues. When I call GetPawn() within this function

 void Auser_control::MoveUp(float Value)
 {
     if (Value != 0.0f && (GetPawn() != NULL))
     {
         FRotator const ControlSpaceRot = GetControlRotation();
         // transform to world space and add it
         GetPawn()->AddMovementInput(FRotationMatrix(ControlSpaceRot).GetScaledAxis(EAxis::Y), Value);
     }
     else
     {
         UE_LOG(LogTemp, Warning, TEXT("NULL")); //ALWAYS RETURNS NULL 
     }
 }

GetPawn() always returns NULL. Please any ideas what do I do wrong?

The whole code you can see down below.

In a GameMode class I set a default Pawn class and a PlayerController class. Like this:

 UCLASS()
 class HANDLECAMERA_API AHandleCameraGameMode : public AGameMode
 {
     GENERATED_BODY()
 
 public:
     AHandleCameraGameMode(const FObjectInitializer& ObjectInitializer);
     
     
 };


  AHandleCameraGameMode::AHandleCameraGameMode(const FObjectInitializer& ObjectInitializer)
     : Super(ObjectInitializer)
 {
     DefaultPawnClass = AMyPawn::StaticClass();
     PlayerControllerClass = Auser_control::StaticClass();
 }


Within the PlayerController class I have next code:

 UCLASS()
 class HANDLECAMERA_API Auser_control : public APlayerController
 {
     GENERATED_BODY()
     
 private:
     UPROPERTY(VisibleDefaultsOnly, Category = "Player Controller")
     USpringArmComponent* SpringArm;
     UPROPERTY(VisibleDefaultsOnly, Category = "Player Controller")
     UCameraComponent* Camera;
     UPROPERTY(VisibleDefaultsOnly, Category = "Player Controller")
     UBoxComponent* CrouchCamera;
     UPROPERTY(VisibleDefaultsOnly, Category = "Player Controller")
     UBoxComponent* BackCamera;
 
 public:
     Auser_control(const FObjectInitializer& ObjectInitializer);
 
     void MoveUp(float Value);
 
     virtual void SetupInputComponent() override;
     virtual void SetPawn(APawn* InPawn) override;
 };

 Auser_control::Auser_control(const FObjectInitializer& ObjectInitializer)
     : Super(ObjectInitializer)
 {
     SpringArm = ObjectInitializer.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("SpringArm"));
     SpringArm->TargetArmLength = 100.f;
     SpringArm->bUsePawnControlRotation = false;
     SpringArm->bInheritPitch = true;
     SpringArm->bInheritYaw = true;
     SpringArm->bInheritRoll = true;
     SpringArm->bEnableCameraLag = false;
     SpringArm->bEnableCameraRotationLag = false;
 
     Camera = ObjectInitializer.CreateDefaultSubobject<UCameraComponent>(this, TEXT("Camera"));
     Camera->AttachTo(SpringArm, USpringArmComponent::SocketName);
     Camera->SetRelativeLocation(FVector(0.f, 0.f, 0.f), false);
 
     CrouchCamera = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, TEXT("CrouchCamera"));
     SpringArm->AttachTo(CrouchCamera);
 }
 
 void Auser_control::SetPawn(APawn* InPawn)
 {
     Super::SetPawn(InPawn);
 }
 
 void Auser_control::SetupInputComponent()
 {
     Super::SetupInputComponent();
 
     check(InputComponent);
 
     if (InputComponent != NULL)
     {
         InputComponent->BindAxis("MoveUp", this, &Auser_control::MoveUp);
     }
 }
 
 void Auser_control::MoveUp(float Value)
 {
     if (Value != 0.0f && (GetPawn() != NULL))
     {
         FRotator const ControlSpaceRot = GetControlRotation();
         // transform to world space and add it
         GetPawn()->AddMovementInput(FRotationMatrix(ControlSpaceRot).GetScaledAxis(EAxis::Y), Value);
     }
     else
     {
         UE_LOG(LogTemp, Warning, TEXT("NULL")); //ALWAYS RETURNS NULL 
     }
 }


And my Pawn class looks like this:

 UCLASS()
 class HANDLECAMERA_API AMyPawn : public APawn
 {
     GENERATED_BODY()
 
 private:
     UPROPERTY(VisibleDefaultsOnly, BluePrintReadOnly, meta = (AllowPrivateAccess = "true"))
     class USceneComponent* DummyRoot;
     UPROPERTY(VisibleDefaultsOnly, BluePrintReadOnly, meta = (AllowPrivateAccess = "true"))
     class USkeletalMeshComponent* SkeletalMesh;
 
 public:
     // Sets default values for this pawn's properties
     AMyPawn();
 
     // Called when the game starts or when spawned
     virtual void BeginPlay() override;
     
     // Called every frame
     virtual void Tick( float DeltaSeconds ) override;
 
     // Called to bind functionality to input
     virtual void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
 
     
     
 };


 AMyPawn::AMyPawn()
 {
     DummyRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Dummy0"));
     RootComponent = DummyRoot;
 
     class USkeletalMesh* TmpSkeletalMesh;
     TmpSkeletalMesh = ConstructorHelpers::FObjectFinderOptional<USkeletalMesh>(TEXT("SkeletalMesh'/Game/MixamoAnimPack/Mixamo_Vanguard/Mesh/Vanguard_by_T__Choonyung.Vanguard_by_T__Choonyung'")).Get();
 
     SkeletalMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("SkeletalMesh0"));
     SkeletalMesh->SetSkeletalMesh(TmpSkeletalMesh);
     SkeletalMesh->AttachTo(RootComponent);
 
      // Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
 
 }
 
 // Called when the game starts or when spawned
 void AMyPawn::BeginPlay()
 {
     Super::BeginPlay();
     
 }
 
 // Called every frame
 void AMyPawn::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
 
 }
 
 // Called to bind functionality to input
 void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
 {
     Super::SetupPlayerInputComponent(InputComponent);
 
 }
Product Version: UE 4.10
Tags:
more ▼

asked Jun 03 '16 at 04:56 PM in C++ Programming

avatar image

Mikhail_G
3 1 2 3

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

1 answer: sort voted first

You got two checks there GetPawn() != NULL and Value != 0.0f

So if your Input Value is 0.0f you will get the Log that says NULL and I assume your Code still works and your Pawn Moves? To Fix this do the Value Check after the Pawn Check like this:

 if (GetPawn() != NULL)
      {
          if(Value != 0.0f)
          {
          FRotator const ControlSpaceRot = GetControlRotation();
          // transform to world space and add it
          GetPawn()->AddMovementInput(FRotationMatrix(ControlSpaceRot).GetScaledAxis(EAxis::Y), Value);
          }
      }

If not than your Pawn is indeed NULL and your Controller somewhere never Possed the Pawn. Let me know if you still get the Log message before I investigate the issue further.

more ▼

answered Jun 03 '16 at 09:11 PM

avatar image

Nachtmahr
6.9k 143 26 172

avatar image Mikhail_G Jun 04 '16 at 08:49 PM

Yes you were right the problem was in the other place.

(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