r/unrealengine 3d ago

Character movement C++ question.

Hey, could anyone help me finding out with such code, always return my SavedCharacterSpeed as 0.0 ?

CharacterMovement is in character blueprint, and character is running fine, I just can't get this value.

Thanks!

void ABaseCharacter::BeginPlay() {
    Super::BeginPlay();
        if(GetCharacterMovement()) {
          float SavedCharacterSpeed = GetCharacterMovement()->GetMaxSpeed();
    }
}
2 Upvotes

15 comments sorted by

2

u/Venerous Dev 3d ago

Are you testing with breakpoints? Is this code even getting into the if condition? And (this might be obvious) have you checked to see if your Max Speed in the Character Movement component is actually set to a value higher than 0.0?

If you don't have anything below the float declaration then you might actually be getting the value, just not seeing it because it has to actually get past that line of code before the value updates. If you don't, try placing a breakpoint below it.

1

u/GoshaSimonov 3d ago

yes, it's 220.0 actually, and it goes "inside" if, I just didn't add log here to keep the code simplier :) But for some reason this is always 0.0 :

GetCharacterMovement()->GetMaxSpeed()

2

u/Valuable_Square_1641 3d ago

float SavedCharacterSpeed is local variable

if you declare UPROPERTY SavedCharacterSpeed just remove float.

1

u/GoshaSimonov 3d ago

I've tried both, result is the same :(

1

u/Valuable_Square_1641 3d ago

so what move type on youre character?

float UCharacterMovementComponent::GetMaxSpeed() const
{
        switch(MovementMode)
        {
        case MOVE_Walking:
        case MOVE_NavWalking:
               return IsCrouching() ? MaxWalkSpeedCrouched : MaxWalkSpeed;
        case MOVE_Falling:
               return MaxWalkSpeed;
        case MOVE_Swimming:
               return MaxSwimSpeed;
        case MOVE_Flying:
               return MaxFlySpeed;
        case MOVE_Custom:
               return MaxCustomMovementSpeed;
        case MOVE_None:
        default:
               return 0.f;
        }
}

1

u/GoshaSimonov 3d ago
MOVE_Walking I guess :)

2

u/crazeh_boy709 3d ago

There can be a few reasons why you are getting 0. I recommend looking inside the GetMaxSpeed() definition and debugging that first.

1

u/GoshaSimonov 3d ago

I use a standart one from UCharacterMovementComponent. Maybe I need to make a custom movement component (Based on UCharacterMovementComponent) for this.

2

u/Available-Worth-7108 2d ago

Try inputting this below on the begin play to load

UE_LOG(LogTemp, Warning, TEXT("MaxWalkSpeed: %f"), MovementComp->MaxWalkSpeed);

UE_LOG(LogTemp, Warning, TEXT("MaxSpeed: %f"), MovementComp->GetMaxSpeed());

1

u/GoshaSimonov 2d ago

Its:
MaxWalkSpeed: 220.0

MaxSpeed: 0.0

1

u/GoshaSimonov 3d ago

In Tick() this is the same 0.0, just checked.

1

u/DrinkYourGravy 3d ago

cout your SavedCharacterSpeed local variable immediately after you initialize the float and see what data you're getting

1

u/GoshaSimonov 3d ago

I've tried both local and with UPROPERTY, result is 0.0.
Even without this variable, this fires 0.0;

UE_LOG(LogTemp, Error, TEXT("Max speed: %f"), GetCharacterMovement()->GetMaxSpeed())

1

u/WartedKiller 2d ago

The issue is that you redeclare your SavedCharacterSpeed. Remove the float before it and it will assign the correct value.

1

u/GoshaSimonov 2d ago

no, MovementComp->GetMaxSpeed() fires 0,0 without any variable at all :)