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"

How can I create a player input component in an actor class?

Hi UE4 community!

So I'm experimenting with one of Unreal Docs tutorials, the Game-Controlled Camera one. I first modified the camera-switching conditional in the Tick() so that it smoothly runs between Camera One and Camera Two, instead of just blending to Camera Two then jerking back to Camera One. That worked fine. Then, to challenge myself, I wanted to try making it depend on a button prompt: pressing x would switch the cameras instead of a timer. It seemed simple enough, but I now realize that since the CameraDirector's parent class is the actor and not the pawn, there is no player input component, so my 'Super::SetupPlayerInputComponent(InputComponent)' is triggering a syntax error (line 88). Is there any way to create the input component? Here is my code:

CameraDirector.h

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #pragma once
 
 #include "GameFramework/Actor.h"
 #include "CameraDirector.generated.h"
 
 UCLASS()
 class HOWTO_AUTOCAMERA_API ACameraDirector : public AActor
 {
     GENERATED_BODY()
     
 public:    
     // Sets default values for this actor's properties
     ACameraDirector();
 
     // 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);
 
     
     //These makes it possible to drag and drop cameras into the class
     UPROPERTY(EditAnywhere)
     AActor* CameraOne;
     
     UPROPERTY(EditAnywhere)
     AActor* CameraTwo;
     
     float TimeToNextCameraChange;
     
     //Button for switching camera manually
     //Input functions
     void StartSwitch();
     void StopSwitch();
     
     //Input variable
     bool bSwitching;
     
     
 };
 

CameraDirector.cpp:

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "HowTo_AutoCamera.h"
 #include "CameraDirector.h"
 #include "Kismet/GameplayStatics.h"
 
 
 // Sets default values
 ACameraDirector::ACameraDirector()
 {
      // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
     PrimaryActorTick.bCanEverTick = true;
     
     //Set this up to be controlled by the lowest-numbered player
     //AutoPosessPlayer = EAutoReceiveInput::Player0;
     
     
 
 }
 
 // Called when the game starts or when spawned
 void ACameraDirector::BeginPlay()
 {
     Super::BeginPlay();
     
 }
 
 // Called every frame
 void ACameraDirector::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
     
     const float SmoothBlendTime = 0.75f;
     if(bSwitching)
     {
         //Locates the player actor
         APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
         if(OurPlayerController)
         {
             if((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
             {
                 //Cut instantly to CameraONe
                 OurPlayerController->SetViewTargetWithBlend(CameraOne, SmoothBlendTime);
             }
             else if((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
             {
                 OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
             }
         }
 
     }
     
     
     /* All of this commented out is from the original tutorial
     const float TimeBetweenCameraChanges = 2.0f;
     const float SmoothBlendTime = 0.75f;
     
     
     TimeToNextCameraChange -= DeltaTime;
     
     if(TimeToNextCameraChange <= 0.0f)
     {
         TimeToNextCameraChange += TimeBetweenCameraChanges;
         
         
         //Locates the player actor
         APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
         if(OurPlayerController)
         {
             if((OurPlayerController->GetViewTarget() != CameraOne) && (CameraOne != nullptr))
             {
                 //Cut instantly to CameraONe
                 OurPlayerController->SetViewTargetWithBlend(CameraOne, SmoothBlendTime);
             }
             else if((OurPlayerController->GetViewTarget() != CameraTwo) && (CameraTwo != nullptr))
             {
                 OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
             }
         }
     }*/
     
 
 }
 
 // Called to bind functionality to input
 void ACameraDirector::SetupPlayerInputComponent(class UInputComponent* InputComponent)
 {
     Super::SetupPlayerInputComponent(InputComponent);
     
     // Respond when the "Switch" key is pressed or released
     InputComponent->BindAction("Switch", IE_Pressed, this, &ACameraDirector::StartSwitch);
     InputComponent->BindAction("Switch", IE_Released, this, &ACameraDirector::StopSwitch);
     
     
 }
 
 void ACameraDirector::StartSwitch()
 {
     bSwitching = true;
 }
 
 void ACameraDirector::StopSwitch()
 {
     bSwitching = false;
 }
 


Product Version: UE 4.15
Tags:
more ▼

asked Jun 05 '17 at 11:14 PM in C++ Programming

avatar image

ghost.robby
3 1 3 5

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

1 answer: sort voted first

Well, in a word, no.

Input components don't make sense on objects that don't somehow belong to the player. If they're not the player (i.e., a player controller, or a pawn possessed by a player controller) then who are they getting this input from? Since Controllers can't possess Actors (this is the defining characteristic which separates an actor form a pawn) then InputComponents don't make sense to be on Actors.

You need to leave your input stuff on the pawn, and then use that input in the camera director. For example, you could create a function on the pawn which takes in a pointer to an ACameraDirector, and then binds actions from the player's input component to the ACameraDirector's StartSwitch and StopSwitch functions. Then, in the level blueprint, when the player spawns, call the function and pass it an ACameraDirector which you've preplaced in the level.

more ▼

answered Jun 05 '17 at 11:28 PM

avatar image

Teiwaz
629 26 29 55

avatar image ghost.robby Jun 06 '17 at 12:21 AM

I had a feeling this was the case. Is it possible to repeat the tutorial with a pawn class instead of an actor class? As for your suggestion, how do I set up the pointer in that function? I've yet to do anything that invokes functions or variables from other scripts.

avatar image Teiwaz Jun 06 '17 at 12:25 AM

You can just change your CameraDirector to inherit from Pawn.

Change class HOWTO_AUTOCAMERA_API ACameraDirector : public AActor

To

 class HOWTO_AUTOCAMERA_API ACameraDirector : public APawn

And

  #include "GameFramework/Actor.h"

to

  #include "GameFramework/Pawn.h"

and then recompile.

avatar image ghost.robby Jun 06 '17 at 12:53 AM

It's that simple? So I added those lines and recompiled: successful! So now, when I hit play, I take control of the CameraDirector, and I'm seeing through the CameraDirector's POV (I'm guessing game objects inherit a camera component automatically from the pawn parent class?). Is there a way to establish my POV as either of my two cameras?

avatar image Teiwaz Jun 06 '17 at 01:42 AM

There should be a SetViewTarget function on the PlayerController. Camera documentation is here: https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/UsingCameras/index.html

(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