c++ – Utilizing UPawnMovementComponent to manage a Pawn with Keyboard sharing code between Pawns

c++ – Utilizing UPawnMovementComponent to manage a Pawn with Keyboard sharing code between Pawns

[ad_1]

I’ve simply began to study Unreal and studying the tutorial Parts and Collisions I’ve discovered that they use the UPawnMovementComponent to manage their Pawn.

Earlier than studying this tutorial I’ve added the code to maneuver the Pawn inside its C++ code:

#embody "Paddle.h"
#embody "Parts/BoxComponent.h"
#embody "Parts/StaticMeshComponent.h"

// Units default values
APaddle::APaddle()
{
    // Set this pawn to name Tick() each body.  You'll be able to flip this off to enhance efficiency in the event you do not want it.
    PrimaryActorTick.bCanEverTick = true;

    // Set this pawn to be managed by the lowest-numbered participant
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    VisualComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("VisualComp"));
    RootComponent = VisualComponent;

    // Eliminated for brevety

    CurrentVelocity.Z = 0.0f;

}

// Known as when the sport begins or when spawned
void APaddle::BeginPlay()
{
    Tremendous::BeginPlay();
    
}

// Known as each body
void APaddle::Tick(float DeltaTime)
{
    Tremendous::Tick(DeltaTime);

    // Deal with motion primarily based on our "MoveZ" axis.
    if (!CurrentVelocity.IsZero())
    {
        const FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
        SetActorLocation(NewLocation);
    }

}

// Known as to bind performance to enter
void APaddle::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
    Tremendous::SetupPlayerInputComponent(PlayerInputComponent);

    // Reply each body to the values of our motion.
    InputComponent->BindAxis(TEXT("MovePaddle"), this, &APaddle::Move_ZAxis);
}

void APaddle::Move_ZAxis(float AxisValue)
{
    CurrentVelocity.Z = FMath::Clamp(AxisValue, -1.0f, 1.0f) * 100.0f;
}

A Tip from that tutorial says:

Pawn Motion Parts have some highly effective, built-in options to assist with frequent physics performance, and are a great way to share motion code between many Pawn varieties. Utilizing Parts to separate performance is an effective follow to scale back litter as your undertaking grows and your Pawns turn out to be extra complicated.

They are saying that utilizing Pawn Motion Parts is an effective technique to share motion code between many Pawn however they’ve the identical code than me inside their Pawn part:

// Copyright 1998-2018 Epic Video games, Inc. All Rights Reserved.

#embody "CollidingPawn.h"
#embody "CollidingPawnMovementComponent.h"
#embody "UObject/ConstructorHelpers.h"
#embody "Particles/ParticleSystemComponent.h"
#embody "Parts/SphereComponent.h"
#embody "Digicam/CameraComponent.h"
#embody "GameFramework/SpringArmComponent.h"

// Units default values
ACollidingPawn::ACollidingPawn()
{
    // Set this pawn to name Tick() each body.  You'll be able to flip this off to enhance efficiency in the event you do not want it.
    PrimaryActorTick.bCanEverTick = true;

    // Eliminated for brevety

    // Take management of the default participant
    AutoPossessPlayer = EAutoReceiveInput::Player0;

    // Create an occasion of our motion part, and inform it to replace our root part.
    OurMovementComponent = CreateDefaultSubobject<UCollidingPawnMovementComponent>(TEXT("CustomMovementComponent"));
    OurMovementComponent->UpdatedComponent = RootComponent;
}

// Known as when the sport begins or when spawned
void ACollidingPawn::BeginPlay()
{
    Tremendous::BeginPlay();

}

// Known as each body
void ACollidingPawn::Tick( float DeltaTime )
{
    Tremendous::Tick( DeltaTime );

}

// Known as to bind performance to enter
void ACollidingPawn::SetupPlayerInputComponent(class UInputComponent* InInputComponent)
{
    Tremendous::SetupPlayerInputComponent(InInputComponent);

    InInputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);

    InInputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);
    InInputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);
    InInputComponent->BindAxis("Flip", this, &ACollidingPawn::Flip);
}

UPawnMovementComponent* ACollidingPawn::GetMovementComponent() const
{
    return OurMovementComponent;
}

void ACollidingPawn::MoveForward(float AxisValue)
{
    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
    {
        OurMovementComponent->AddInputVector(GetActorForwardVector() * AxisValue);
    }
}

void ACollidingPawn::MoveRight(float AxisValue)
{
    if (OurMovementComponent && (OurMovementComponent->UpdatedComponent == RootComponent))
    {
        OurMovementComponent->AddInputVector(GetActorRightVector() * AxisValue);
    }
}

void ACollidingPawn::Flip(float AxisValue)
{
    FRotator NewRotation = GetActorRotation();
    NewRotation.Yaw += AxisValue;
    SetActorRotation(NewRotation);
}

void ACollidingPawn::ParticleToggle()
{
    if (OurParticleSystem && OurParticleSystem->Template)
    {
        OurParticleSystem->ToggleActive();
    }
}

They’ve the identical SetupPlayerInputComponent methodology and likewise three strategies to maneuver the Pawn contained in the Pawn class. I do not know the way they’ll share transfer code between Pawn if the code is inside every Pawn and the way we are able to separate performance if all of the motion code is contained in the Pawns.

How can I take advantage of UPawnMovementComponent to manage a Pawn with Keyboard sharing that code with different Pawns?

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply