0
\$\begingroup\$

I'm creating a my own movement component inherited by UCharacterMovementComponent. I should implement a scale factor (I called it JumpZVelocityScale) to apply the JumpZVelocity (and I don't want change the engine source code). So my idea is to override the methods of UCharacterMovementComponent which use the JumpZVelocity variable, multiply it by JumpZVelocityScale and to restore it after I just called the parent implementation. Since some methods are const, I had to change their access modifiers and I implemented the non-const overloads. So I would like to have an opinion from you about this implementation and to know if I can improve it.

Header:

/* MyCharacterMovementComponent.h */
#pragma once
#include "GameFramework/CharacterMovementComponent.h"
#include "MyCharacterMovementComponent.generated.h"

UCLASS(Blueprintable, BlueprintType)
class UMyCharacterMovementComponent : public UCharacterMovementComponent
{
    GENERATED_BODY()

public:

    FORCEINLINE void SetJumpZVelocityScale(float InJumpZVelocityScale) { JumpZVelocityScale = /*FMath::Clamp(*/InJumpZVelocityScale, 0.0f, 1.0f/*)*/; }

    FORCEINLINE float GetJumpZVelocityScale() const { return JumpZVelocityScale; }

    /* overriden this method to apply the JumpZVelocityScale*/
    virtual bool DoJump(bool bReplayingMoves) override;

    /* overriden this method to apply the JumpZVelocityScale*/
    virtual void JumpOff(AActor* MovementBaseActor) override;

    /* overriden this method to apply the JumpZVelocityScale*/
    virtual void PhysFalling(float deltaTime, int32 Iterations) override;

    /* non-const overload where I apply the JumpZVelocityScale*/
    float GetMaxJumpHeight();

    /* non-const overload where I apply the JumpZVelocityScale*/
    float GetMaxJumpHeightWithJumpTime();

protected:

    UPROPERTY(Category = "Character Movement: Jumping Scale Factor", EditAnywhere, BlueprintReadWrite, meta = (DisplayName = "Jump Z Velocity Scale", ClampMin = "0", UIMin = "0"))
    float JumpZVelocityScale{ 1.0f };

private:

    /* this method is overriden and move into the private section to re-implement as a non-const overload */
    virtual float GetMaxJumpHeight() const override;

    /* this method is overriden and move into the private section to re-implement as a non-const overload */
    virtual float GetMaxJumpHeightWithJumpTime() const override;
};

Source:

/* MyCharacterMovementComponent.cpp */
#include "MyCharacterMovementComponent.h"

bool UMyCharacterMovementComponent::DoJump(bool bReplayingMoves)
{
    bool Result{};
    auto PrevJumpZVelocity = JumpZVelocity;
    JumpZVelocity = JumpZVelocity * JumpZVelocityScale;

    Result = Super::DoJump(bReplayingMoves);

    JumpZVelocity = PrevJumpZVelocity;

    return Result;
}

void UMyCharacterMovementComponent::JumpOff(AActor * MovementBaseActor)
{
    auto PrevJumpZVelocity = JumpZVelocity;
    JumpZVelocity = JumpZVelocity * JumpZVelocityScale;

    Super::JumpOff(MovementBaseActor);

    JumpZVelocity = PrevJumpZVelocity;
}

void UMyCharacterMovementComponent::PhysFalling(float deltaTime, int32 Iterations)
{
    auto PrevJumpZVelocity = JumpZVelocity;
    JumpZVelocity = JumpZVelocity * JumpZVelocityScale;

    Super::PhysFalling(deltaTime, Iterations);

    JumpZVelocity = PrevJumpZVelocity;
}

float UMyCharacterMovementComponent::GetMaxJumpHeight()
{
    float Result{};

    auto PrevJumpZVelocity = JumpZVelocity;
    JumpZVelocity = JumpZVelocity * JumpZVelocityScale;

    const ThisClass& ConstThisRef = *this;
    Result = ConstThisRef.GetMaxJumpHeight();

    JumpZVelocity = PrevJumpZVelocity;

    return Result;
}

float UMyCharacterMovementComponent::GetMaxJumpHeightWithJumpTime()
{
    float Result{};

    auto PrevJumpZVelocity = JumpZVelocity;
    JumpZVelocity = JumpZVelocity * JumpZVelocityScale;

    const ThisClass& ConstThisRef = *this;
    Result = ConstThisRef.GetMaxJumpHeightWithJumpTime();

    JumpZVelocity = PrevJumpZVelocity;

    return Result;
}

float UMyCharacterMovementComponent::GetMaxJumpHeight() const
{
    return Super::GetMaxJumpHeight();
}

float UMyCharacterMovementComponent::GetMaxJumpHeightWithJumpTime() const
{
    return Super::GetMaxJumpHeightWithJumpTime();
}
\$\endgroup\$
  • \$\begingroup\$ If it works -> dont fix it. \$\endgroup\$ – ColdSteel Oct 26 '19 at 23:38
  • \$\begingroup\$ Thank you for your comment ColdSteel, but I don't think the "if it works then it's ok" philosofy is good approach to improve myself as a programmer \$\endgroup\$ – Radioga Oct 28 '19 at 18:29
  • \$\begingroup\$ Your code looks ok, I cant say that I would do it the same way. But it is pretty ok. \$\endgroup\$ – ColdSteel Oct 28 '19 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.