Hello!
I would love some help with problem I’ve run into. I want to make a baseclass for enemies and the player to store health values and other things i might create there. I wanted to put the setup player input component function in the player/Aantagonist but it doesn’t seem to work. It does however work when i put the code in Basecharacter the parent and spawn in that character as a blueprint.
Thanks so much.
Could it be because you’re missing
Super::SetupPlayerInputComponent(PlayerInputComponent)
?
1 Like
That didn’t seem to be the problem. Ty for replying
This works just change the api and test it
BaseCharacter.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "BaseCharacter.generated.h"
UCLASS()
class DROID_API ABaseCharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
ABaseCharacter();
UPROPERTY(EditAnywhere)
float Health;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
BaseCharacter.cpp
#include "BaseCharacter.h"
// Sets default values
ABaseCharacter::ABaseCharacter()
{
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ABaseCharacter::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ABaseCharacter::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
Antagonist.h
#pragma once
#include "CoreMinimal.h"
#include "BaseCharacter.h"
#include "Antagonist.generated.h"
/**
*
*/
UCLASS()
class DROID_API AAntagonist : public ABaseCharacter
{
GENERATED_BODY()
public:
AAntagonist();
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
UPROPERTY(EditAnywhere)
class USpringArmComponent* SpringArm;
UPROPERTY(EditAnywhere)
class UCameraComponent* Camera;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION()
void MoveForward(float Value);
UFUNCTION()
void MoveRight(float Value);
};
Antagonist.cpp
#include "Antagonist.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Components/ArrowComponent.h"
AAntagonist::AAntagonist()
{
GetArrowComponent()->SetHiddenInGame(false);
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("Spring Arm"));
SpringArm->SetupAttachment(GetRootComponent());
SpringArm->SetRelativeRotation(FRotator(-90, 0, 0));
SpringArm->TargetArmLength = 400;
SpringArm->bEnableCameraLag = true;
SpringArm->CameraLagSpeed = 1;
Camera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera"));
Camera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
}
void AAntagonist::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
PlayerInputComponent->BindAxis("MoveForward", this, &AAntagonist::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AAntagonist::MoveRight);
}
void AAntagonist::MoveForward(float Value)
{
const FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetUnitAxis(EAxis::X);
AddMovementInput(Direction, Value);
if (Value != 0) {
FString message = "Forward " + Direction.ToString() + " " + FString::SanitizeFloat(Value);
GEngine->AddOnScreenDebugMessage(-1, 0.3f, FColor::Cyan, message);
}
}
void AAntagonist::MoveRight(float Value)
{
const FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetUnitAxis(EAxis::Y);
AddMovementInput(Direction, Value);
if (Value != 0) {
FString message = "Right " + Direction.ToString() + " " + FString::SanitizeFloat(Value);
GEngine->AddOnScreenDebugMessage(-1, 0.3f, FColor::Orange, message);
}
}
// Called when the game starts or when spawned
void AAntagonist::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AAntagonist::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
Here it is in a project
2 Likes
Thanks so much, I believe not calling the super::tick in Antagonist caused the problem. <3