A newbie looking for a guidance

Hello all,

First of all, i am noob and i am trying to to learn c++ just for fun. So, whats is my problem… I am trying to make a function that stops the running character, and turns him around after he stops.
I have a working piece of code, but i think theres a better way how to make it work, but i just can’t see it.

I will show you what i mean:


void AMyCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Super::SetupPlayerInputComponent(PlayerInputComponent);

    check(PlayerInputComponent);   

    // List of Default Axis Bindings
    PlayerInputComponent->BindAxis("TurnAround", this, &AMyCharacter::TurnAround);
}

namespace
{
    UPathFollowingComponent* InitNavigationControl(AController& Controller)
    {
        AAIController* AsAIController = Cast<AAIController>(&Controller);
        UPathFollowingComponent* PathFollowingComp = nullptr;

        if (AsAIController)
        {
            PathFollowingComp = AsAIController->GetPathFollowingComponent();
        }
        else
        {
            PathFollowingComp = Controller.FindComponentByClass<UPathFollowingComponent>();
            if (PathFollowingComp == nullptr)
            {
                PathFollowingComp = NewObject<UPathFollowingComponent>(&Controller);
                PathFollowingComp->RegisterComponentWithWorld(Controller.GetWorld());
                PathFollowingComp->Initialize();
            }
        }

        return PathFollowingComp;
    }
}

// FIX ME
void AMyCharacter::TurnAround(float AxisRate)
{
    //
    static UPathFollowingComponent* PathFollowingComponent = InitNavigationControl(*Controller);

    //
    static bool bIsFollowingPath = true;

    if (Controller != NULL && AxisRate)
    {
        //
        if (bIsFollowingPath)
        {
            // Cancel MouseClick Movement
            Controller->StopMovement();

            //
            bIsFollowingPath = false;
        }

        //
        if (PathFollowingComponent->GetStatus() != EPathFollowingStatus::Moving)
        {
            // Move the Camera around the Character
            float DeltaYaw = AxisRate * BaseTurnAroundRate * UGameplayStatics::GetWorldDeltaSeconds(this);
            AddControllerYawInput(DeltaYaw);

            // And move the character the same way, while standing still
            if (!InputComponent->GetAxisValue("MoveForward") || !InputComponent->GetAxisValue("MoveRight"))
            {
                FRotator NewActorRotation = GetActorRotation();
                NewActorRotation.Yaw += DeltaYaw * CastChecked<APlayerController>(Controller)->InputYawScale;
                SetActorRotation(NewActorRotation);
            }
        }

        /*/ Should we done this in the Animation Blueprint instead?
        if (WalkingAnimation != NULL)
        {
            // Get the animation object for the arms mesh
            UAnimInstance* AnimInstance = GetMesh()->GetAnimInstance();
            if (AnimInstance != NULL)
            {
                GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, FString::Printf(TEXT("Character Velocity (X:%f, Y:%f, Z:%f)"), GetVelocity().X, GetVelocity().Y, GetVelocity().Z));
                GetMesh()->PlayAnimation(WalkingAnimation, true);
                //AnimInstance->Montage_Play(WalkingAnimation, 1.f, EMontagePlayReturnType::Duration);
            }
        }*/
    }
    else
    {
        //
        bIsFollowingPath = true;
    }
}

As you can see, my fuction TurnAround is bound to my project axis binding, so it’s executed every tick… that means that my static bool bIsFollowingPath is set to True every tick, when i am not pressing the keyboard button.

A) Is it possible to rewrite the function, so it doesn’t do that, and still works in the same way?
B) I am using UPathFollowingComponent here, and the only way how to get the used UPathFollowingComponent is via UPathFollowingComponent* InitNavigationControl(AController& Controller) pointer function that i copy-paste from the UAIBlueprintHelperLibrary… I had to copy-paste it, because i couldn’t find an accessible version. I also write it as static in the TurnAround function, i hope that is not a problem?
C) I am using a casting there, which i don’t understand much, but since its a another copy-paste, i think it’s ok?
D) I also want to add an animation while the character is turning, but as i wrote in the commented section, i believe i should done that in the animation bleuprint, not here.

You wont likely get a decent response. I’ll let you know why. And its about respecting the proper channels.

  1. This is not a site for learning c++. Unreal is a terrible platform for learning c++. There are tons of sites where professionals DONATE time to help you with this.
  2. This is a site for unreal questions. Your question is about math.
  3. You need to limit your Unreal Question(s) to a single topic. not, “How can I rewrite unreal?”, “Whats a Cast?”, and “How do I animate?”…
  4. Ultimately you are asking, “How do I game play program?”. And this is not the place for that. Try https://www.gamedev.net/. or for c++ specific stuff https://bytes.com/ or ask on StackOverflow.

I completely understand your frustration and the fact that you are inundated with so much to learn, and want to do so in such a short time.

There is a large community of us willing to help. You need to help US help YOU.

Take the time to organize your thoughts. Break it down into small solvable questions. And then please ask them on the proper forums.

Happy Coding!

I second what Hollingsworth said. UE4 is an insanely complex piece of software. Answering just one question might take a team of brainstorming devs. Answering four, on different topics, in a coherent way, is just tough.

There’s nothing wrong with copy-pasting to get up and running, but then you should go back and understand what you pasted. I would start by learning about casting. It’s fundamental to c++, for game dev or otherwise.