Crash when I add NPC class, from a exempla

Hi, I’m learning C ++ along with the UE, through the book " Learning C ++ by Creating Games with EU4 ." In this book he is using the 4.5.1 version and I 'm using the 4.8.3 version. Is there a problem?

For example, in chapter 8 "Actors and Pawns " when he will compile the NPC code, the following error appears “error C2535: 'void AAvatar :: SetupPlayerInputComponent ( UInputComponent *) ': member function already defined or declared.”

And before that, has some happens that I found strange, but so far came thinking it was some lack of my knowledge. When I press the play button instead of the camera is on my avatar (as the book asks me to put), it creates another avatar in another position with the same camera angle, sometimes falling from the platform depending on the position of my vision when I press the play button.

Could anyone help me these questions? Thanks :slight_smile:

If you post the code you’re having issues with, I’ll make sure to look into it.

Ok!

This’s the Avatar.h class
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “GameFramework/Character.h”
#include “Avatar.generated.h”

UCLASS()
class GOLDENEGG_API AAvatar : public ACharacter
{
GENERATED_UCLASS_BODY()

void SetupPlayerInputComponent(class UInputComponent* InputComponent) override;
void MoveForward(float amount);
void MoveRight(float amount);
void MoveLeft(float amount);
void MoveBack(float amount);
void Yaw(float amount);
void Pitch(float amount);

public:
// Sets default values for this character’s properties
AAvatar();

// 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;

};

And this, Avatar.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include “GoldenEgg.h”
#include “Avatar.h”

// Sets default values
AAvatar::AAvatar()
{
// 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;

}

// Called when the game starts or when spawned
void AAvatar::BeginPlay()
{
Super::BeginPlay();

}

// Called every frame
void AAvatar::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );

}

// Called to bind functionality to input
void AAvatar::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);

check(InputComponent);
InputComponent->BindAxis("Forward", this, &AAvatar::MoveForward);
InputComponent->BindAxis("Strafe", this, &AAvatar::MoveRight);
InputComponent->BindAxis("Yaw", this, &AAvatar::Yaw);
InputComponent->BindAxis("Pitch", this, &AAvatar::Pitch);

}

void AAvatar::MoveForward(float amount){
if (Controller && amount){
FVector fwd = GetActorForwardVector();
AddMovementInput(fwd, amount);
}
}

void AAvatar::MoveRight(float amount){
if (Controller && amount){
FVector right = GetActorRightVector();
AddMovementInput(right, amount);
}
}

void AAvatar::MoveLeft(float amount){
if (Controller && amount){
FVector left = -GetActorRightVector();
AddMovementInput(left, amount);
}
}

void AAvatar::MoveBack(float amount){
if (Controller && amount){
FVector back = -GetActorForwardVector();
AddMovementInput(back, amount);
}
}

void AAvatar::Yaw(float amount){
AddControllerYawInput(200.f * amount * GetWorld()->GetDeltaSeconds());
}

void AAvatar::Pitch(float amount){
AddControllerPitchInput(200.f * amount * GetWorld()->GetDeltaSeconds());
}

Alright, just give me one moment to check into it. Currently, I’m having to run a system update first.

Ok, thanks! :slight_smile:

I’m just posting again, so that you’ll see this reply. I had a dumb moment and you’ll probably feel a little silly after you look at the code again.
You’ve literally defined the SetupPlayerInputComponent function twice. Once as private in the header and once as public in the header.

Out of curiosity, are you perhaps normally a Java developer?
I see several of your functions written like this:

To where the first brace is on the same line as the function name and parameters. Nothing wrong with it, just wanted to see if that is where you’d adopted it from.

Hahaha, you’re completely right! I learned object orientation in java, but I’m in love with C ++ . However , I can not lose the habit of leaving the opening of the keys in the same line of the function , it is visually better for me. And thank you , really I did not realize this error so foolish !

If not too much abuse I have 2 more questions: you could answer my doubts about the UE create another avatar when I play the program?

Does C++ usually goes different? Because i heard java is a downgrade on the resume in game programming jobs, also heard C++ looks great in it so i’m really curious.

Thanks a lot, again!!

Having two avatars sounds like you just have it creating two players? When you hover over the play-in-editor icon, see if you have the number of players set to 2. Usually if you do this, UE4 opens a second window for the other player.

As for C++ vs Java, to be honest with you, I have no idea. About which one companies prefer, both are rather good languages imo.
I just seriously wish C++ would implement some form of reflection (RTTI does not count) already, it’d make a lot of things much, much easier.