Custom Character Movement Component - Epic Wiki

# Custom Character Movement Component

From Epic Wiki

Jump to: navigation, search

# Contents

# 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(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(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.

  1. pragma once

  2. 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.

  1. 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(CharacterMovement);

if(CustomCharMovementComp) {

CustomCharMovementComp->CallFunction();

}

# Conclusion

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

Enjoy!

Rama (talk)

Retrieved from "https://wiki.unrealengine.com/index.php?title=Custom_Character_Movement_Component&oldid=225"

Categories: