Custom Character Movement Component

From Epic Wiki
Jump to: navigation, search

Overview

Author: Rama (talk)

Dear Community,

If you look at CharacterMovementComponent.h the vast majority of the functions are virtual!

This is great news!

You can easily create custom character movement behaviors.... if you can get your characters to use your custom CharacterMovementComponent class!

Here's how you can do it!

Character Constructor (UE >4.6)

First, replace

AVictoryPlayerCharacterBase::AVictoryPlayerCharacterBase(const class FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{	
        //this is your regular constructor code
}

with (where ACharacter::CharacterMovementComponentName is a string of type FName):

AVictoryPlayerCharacterBase::AVictoryPlayerCharacterBase(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer.SetDefaultSubobjectClass<UVictoryCharMoveComp>(ACharacter::CharacterMovementComponentName))
{

Character Constructor (UE <4.6)

Replace

AVictoryPlayerCharacterBase::AVictoryPlayerCharacterBase(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{	
        //this is your regular constructor code
}

with

AVictoryPlayerCharacterBase::AVictoryPlayerCharacterBase(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP.SetDefaultSubobjectClass<UVictoryCharMoveComp>(ACharacter::CharacterMovementComponentName))
{	
        // this is your regular constructor code
}

Header File

Following is an example for the class definition.

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "VictoryCharMoveComp.generated.h"

UCLASS()
class UVictoryCharMoveComp : public UCharacterMovementComponent
{
	GENERATED_UCLASS_BODY()

	
protected:
	
	//Init
	virtual void InitializeComponent() override;
	
	//Tick
	virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
};


C++ Source Code File

// Copyright 1998-2013 Epic Games, Inc. All Rights Reserved.

#include "VictoryGame.h"

//////////////////////////////////////////////////////////////////////////
// UVictoryCharMoveComp

UVictoryCharMoveComp::UVictoryCharMoveComp(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	
}

void UVictoryCharMoveComp::InitializeComponent()
{
	Super::InitializeComponent();
	//~~~~~~~~~~~~~~~~~
	
	//UE_LOG //comp Init!
}

//Tick Comp
void UVictoryCharMoveComp::TickComponent(
	float DeltaTime, 
	enum ELevelTick TickType, 
	FActorComponentTickFunction *ThisTickFunction
){
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	//UE_LOG //custom comp is ticking!!!
	
}


Accessing Custom Character Movement Component

//Inside Character Class

UVictoryCharMoveComp* CustomCharMovementComp = Cast<UVictoryCharMoveComp>(CharacterMovement);

if(CustomCharMovementComp)
{
  CustomCharMovementComp->CallFunction();
}

Conclusion

Now you know how to create entirely custom movement systems for your UE4 Characters!

Enjoy!

Rama (talk)