How do you check for when the movementmode changes?

I’ve had a hell of a time trying to figure out a good way to do this. I need a function to run once when the player lands after falling. I basically have been doing it in the tick function like so:

	if (!GetCharacterMovement()->IsMovingOnGround()) {

		if (GetCharacterMovement()->IsMovingOnGround()) {
			Landing();
		}
	}

But this just decided to stop working for some reason and I figure it’s prob a ■■■■ poor way to do it anyway. I’ve gone down a hell of a Google rabbit hole on this and come up with nothing. Came across OnMovementModeChanged() but can’t get it implemented to save my life. Apparently it doesn’t work in UE5 anymore or something? Any direction would be greatly appreciated.

Custom MovementComp CharacterMovementComponentEX

Header

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

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "CharacterMovementComponentEX.generated.h"

/**
 * 
 */
UCLASS()
class LANDSIGN_API UCharacterMovementComponentEX : public UCharacterMovementComponent
{
	GENERATED_BODY()


		virtual void ProcessLanded(const FHitResult& Hit, float remainingTime, int32 Iterations) override;
};

CPP

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


#include "CharacterMovementComponentEX.h"


void UCharacterMovementComponentEX::ProcessLanded(const FHitResult& Hit, float remainingTime, int32 Iterations) {
	Super::ProcessLanded(Hit, remainingTime, Iterations);
	GEngine->AddOnScreenDebugMessage(-1, 1, FColor::Orange, "Landed");

}

Changes to character in c++ (i called it LandSignCharacter)

Header

// Its CDO
ALandSignCharacter(const FObjectInitializer& ObjectInitializer); 

in cpp

find CDO implementation and extend it swapping movement for the new class


ALandSignCharacter::ALandSignCharacter(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer.SetDefaultSubobjectClass<UCharacterMovementComponentEX>(CharacterMovementComponentName))
{
// your internal cdo code
}

And you character class needs to have the include to the new movement in the cpp

#include "CharacterMovementComponentEX.h"

Files from a quick project:

CharacterMovementComponentEX.cpp (374 Bytes)
CharacterMovementComponentEX.h (478 Bytes)
LandSignCharacter.cpp (4.9 KB)
LandSignCharacter.h (2.0 KB)

Edit: ok wend overkill didn’t notice that the character class too has a landed function :stuck_out_tongue:

Saw your post before you deleted it. Using Landed worked like a charm

1 Like

If I’m understanding this right, in order to use OnMovementModeChanged() then I’d have to create a custom movement mode to begin with?

No, you can override the function in your character class

header

virtual void OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PreviousCustomMode = 0) override;

in cpp

void YourClass::OnMovementModeChanged(EMovementMode PrevMovementMode, uint8 PreviousCustomMode){
Super::OnMovementModeChanged(PrevMovementMode,PreviousCustomMode);

// your code

}

edit : fixed declaration in cpp (copied with default = 0 causing compile error)

Honestly, getting Landed to work made me realize that that’s what I was missing when I tried using OnMovementModeChanged earlier. I didn’t think about declaring the override.

Just be sure to call it’s super counterpart so that the parent’s function is fired too.

1 Like

You answered a question I was literally just looking up too. I keep seeing the Super and wondering what it’s about. Recently shifted from Unity and C# to Unreal and C++ so having to crash course everything. Thank you very much.

No problem. Just watch out for null’s in unreal you can do checks like

  • IsValid() = will check if is not null and not pending kill (so destructor is already called)
  • you can check if != nullptr to check if it’s a null pointer

then there are assets

A couple of ways to skin a cat.

Make sure to look into the newer smart pointers if null crashes will give you a hard time.

C# took care of all of this in unity behind the scenes.

Oh yeah, ran into this face first almost immediately but I have experience in other languages too so was basically like riding a bike and going back to basics.

1 Like