Implementing a custom movement component

Hi I tried making my own character movement component class and I want to make a character class that use it. I tried following this tutorial but I doesn’t seem to work. I get a compiler error saying that the constructor already has a body. Currently I am just trying to get the character to use the right movement component but the plan is to implement custom movement modes. Any help would be much appreciated!

Character Header:

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "AdvCharMovementComponent.h"
#include "AdvancedCharacter.generated.h"

UCLASS()
class ADVCHARPROJ_API AAdvancedCharacter : public ACharacter
{
	GENERATED_BODY()

public:
	// Sets default values for this character's properties
	//AAdvancedCharacter(const FObjectInitializer& ObjectInitializer);

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	// Called to bind functionality to input
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	
	
};

Character Source:

#include "AdvancedCharacter.h"


// Sets default values
AAdvancedCharacter::AAdvancedCharacter(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer.SetDefaultSubobjectClass<UAdvCharMovementComponent>(ACharacter::CharacterMovementComponentName))
{

 	// 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 AAdvancedCharacter::BeginPlay()
{
	Super::BeginPlay();
	
}

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

}

// Called to bind functionality to input
void AAdvancedCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

}

Movement Component Header:

#include "CoreMinimal.h"
#include "EngineGlobals.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "AdvCharMovementComponent.generated.h"

#define PRINT(x) if(GEngine){GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Yellow, TEXT(x));}


UENUM(BlueprintType)
enum ECustomMovementMode
{
	CUSTOM_prone	UMETA(DisplayName = "Prone_mode"),
	CUSTOM_cover	UMETA(DisplayName = "Prone_mode")
};

/**
 * 
 */
UCLASS()
class ADVCHARPROJ_API UAdvCharMovementComponent : public UCharacterMovementComponent
{
	GENERATED_UCLASS_BODY()
	
	
protected:
	virtual void PhysCustom(float deltaTime, int32 Iterations) override;
	void PhysCustomProne(float deltaTime, int32 Iterations);
	void PhysCustomCover(float deltaTime, int32 Iterations);
	
};

Movement Component Source:

#include "AdvCharMovementComponent.h"


void UAdvCharMovementComponent::PhysCustom(float deltaTime, int32 Iterations) {
	switch (CustomMovementMode)
	{
	case CUSTOM_prone:
		PhysCustomProne(deltaTime,Iterations);
		break;
	case CUSTOM_cover:
		PhysCustomCover(deltaTime, Iterations);
		break;
	default:
		break;
	}
}

void UAdvCharMovementComponent::PhysCustomCover(float deltaTime, int32 Iterations) {
	PRINT("Cover");
	PhysWalking(deltaTime, Iterations);
}

void UAdvCharMovementComponent::PhysCustomProne(float deltaTime, int32 Iterations) {
	PRINT("Prone");
	PhysWalking(deltaTime, Iterations);
}
1 Like

You are missing #pragma once in the beginning of your header files.

Check if implementing this guard fixes the error.

No I am not, I just forgot to add them when pasting the code. Sorry about that

You are also missing your component initialization:

//in Movement Component Header:
virtual void InitializeComponent() override;
//in Movement Component Source:
void UAdvCharMovementComponent::InitializeComponent() { Super::InitializeComponent(); }

And your are also missing your component constructor:

//in Movement Component Source:
UAdvCharMovementComponent::UAdvCharMovementComponent(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP){}

I am guessing that by not defining a constructor you get the default constructor which is also present in the generated file. (or something of that sort)

This provides me with the following errors.

I don’t understand why the “super” call is not inside the body of the constructor. I am not very experienced with c++

I managed to fix it myself. It seems that I don’t need to do anything with the movementcomponent constructor, only the Character constructor needed to be modified to AAdvancedCharacter::AAdvancedCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer.SetDefaultSubobjectClass<UAdvCharMovementComponent>(ACharacter::CharacterMovementComponentName)) {}

“super” is outside of the body because it is part of the initializer list. You can move it inside if you want and it will not change anything.

Check all of your .h files. Make sure you don’t have deprecated headers that define a class with the same name.

Double check your constructor.

No the problem wasn’t in the Character constructer but in the movement component constructor. When I changed the movement components constructer back to default it worked. The tutorial I followed was a bit outdated and which caused me to interpret it incorrectly :slight_smile:

You needed to have no instructions in the body of the constructor?

I don’t see any other difference…