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"

Handling input on a per pawn basis in C++

So I'm just starting out with using C++ in UE4 and am trying to make my own character inheriting from the Character class. It compiles fine, but crashes when I hit the play button, and throws me an error saying "Access Violation".

I've been googling for the past 2 hours reading several UE4 AnswerHub questions, but none seem to apply for what I want. They are all telling me to bind my inputs in the PlayerController, but then how am I supposed to use the actual inputs on a per pawn basis (For example different types of characters, one that can fly but another which can't etc.)

I did get it working in Blueprint before but then decided to use C++ as I prefer programming over visual scripting (faster to type than keep clicking everywhere, and is more clear compared to a load of lines everywhere)

Here's my code (the axis used are defined in the project settings):

NovaPlayer.h

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #pragma once
 
 #include "GameFramework/Character.h"
 #include "NovaPlayer.generated.h"
 
 /**
  *
  */
 
 UCLASS()
 class PROJECTNOVATERRA_API ANovaPlayer : public ACharacter
 {
     GENERATED_UCLASS_BODY()
 
     bool isSprinting;
 
 public:
 
     void ANovaPlayer::BeginPlay() override;
 
     void MoveForward(float axisValue);
 
     void MoveRight(float axisValue);
 
     void LookUp(float axisValue);
 
     void LookRight(float axisValue);
 
     void Sprint(float axisValue);
 };

NovaPlayer.cpp

 // Fill out your copyright notice in the Description page of Project Settings.
 
 #include "ProjectNovaTerra.h"
 #include "NovaPlayer.h"
 
 ANovaPlayer::ANovaPlayer(const FObjectInitializer& init)
 : Super(init) {
 }
 
 void ANovaPlayer::BeginPlay() {
     InputComponent->BindAxis("MoveForward", this, &ANovaPlayer::MoveForward);
     InputComponent->BindAxis("MoveRight", this, &ANovaPlayer::MoveRight);
     InputComponent->BindAxis("LookUp", this, &ANovaPlayer::LookUp);
     InputComponent->BindAxis("LookRight", this, &ANovaPlayer::LookRight);
     InputComponent->BindAxis("Sprint", this, &ANovaPlayer::Sprint);
 }
 
 void ANovaPlayer::MoveForward(float axisValue) {
     AddMovementInput(GetActorForwardVector(), isSprinting ? 1 : 0.5f);
 }
 
 void ANovaPlayer::MoveRight(float axisValue) {
     AddMovementInput(GetActorRightVector(), isSprinting ? 1 : 0.5f);
 }
 
 void ANovaPlayer::LookUp(float axisValue) {
     float preClampedPitch = GetActorRotation().Pitch + axisValue;
 
     AddControllerPitchInput(FMath::ClampAngle(preClampedPitch, -90, 90));
 }
 
 void ANovaPlayer::LookRight(float axisValue) {
     AddControllerYawInput(axisValue);
 }
 
 void ANovaPlayer::Sprint(float axisValue) {
     isSprinting = axisValue > 0 ? true : false;
 }

Sidenote: I've never actually used C++ in any other project before, but I believe that I understand it fairly well. So if there's any beginner errors in my code please do point them out!

Product Version: Not Selected
Tags:
more ▼

asked Feb 22 '15 at 06:21 PM in C++ Programming

avatar image

Demozo
26 2 4 7

avatar image MickD777 Feb 22 '15 at 11:01 PM

I find this line weird :

 void ANovaPlayer::BeginPlay() override;

I think it should be:

 virtual void BeginPlay() override;

Maybe this is not the issue, but give it a try. Also, if it still happens, post the Log of when your access violation pops up. It can often help in determining the problem. It should be in YourGame/Saved/Logs.

Mick

avatar image Demozo Feb 23 '15 at 12:20 AM

Is this what you were asking for? http://pastebin.com/5CC1ssq9

avatar image MickD777 Feb 23 '15 at 01:22 AM

Yes. From what I can see, I would say that at the moment of your binding, the InputComponent is not yet created.

In Character, there is a specific function where you should do your binding. If I remember correctly, it is SetupPlayerInputComponent(). So, instead of doing the binding in BeginPlay() you should do in in that function by overriding it.

Basically, in your .h :

 virtual void SetupPlayerInputComponent(UInputComponent* InputComponent) override;

In your cpp:

 void ANovaPlayer::SetupPlayerInputComponent(UInputComponent* InputComponent)
 {
      check(InputComponent);
      InputComponent->BindAxis("MoveForward", this, &ANovaPlayer::MoveForward);
      InputComponent->BindAxis("MoveRight", this, &ANovaPlayer::MoveRight);
      InputComponent->BindAxis("LookUp", this, &ANovaPlayer::LookUp);
      InputComponent->BindAxis("LookRight", this, &ANovaPlayer::LookRight);
      InputComponent->BindAxis("Sprint", this, &ANovaPlayer::Sprint);
 }
avatar image Demozo Feb 23 '15 at 03:30 AM

That did seem to be the issue, thanks! Does "check(InputComponent);" check if it's null? Also, could you turn this into an answer so I can accept it? :)

avatar image asperatology Mar 22 '15 at 05:22 AM

You can reply to yourself and accept that as answer. Don't forget to give credit to where it is due.

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

0 answers: sort voted first
Be the first one to answer this question
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