# Unreal Engine C++ ### C++ Character Class #### Camera and Spring Arm ##### Header File ```cpp= //forward declare class USpringArmComponent; class UCameraComponent; ``` ```cpp= private: UPROPERTY(VisibleAnywhere, Category = Camera) USpringArmComponent* CameraBoom; UPROPERTY(VisibleAnywhere, Category = Camera) UCameraComponent* FollowCamera; UPROPERTY(EditAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true")) class UWidgetComponent* OverheadWidget; ``` ##### CPP File ```cpp= #include "GameFramework/SpringArmComponent.h" #include "Camera/CameraComponent.h" #include "GameFramework/CharacterMovementComponent.h" #include "Components/WidgetComponent.h" ``` ```cpp= CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom")); CameraBoom->SetupAttachment(GetMesh()); CameraBoom->TargetArmLength = 600.f; CameraBoom->bUsePawnControlRotation = true; FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera")); FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); FollowCamera->bUsePawnControlRotation = false; bUseControllerYaw = false; GetCharacterMovement()->bOrientRotationToMovement = true; OverheadWidget = CreateDefaultSubobject<UWidgetComponent>(TEXT("OverheadWidget")); OverheadWidget->SetupAttachment(RootComponent); ``` #### Character Movement ##### Header File ```cpp= protected: void MoveForward(float Value); void MoveRight(float Value); void Turn(float Value); void LookUp(float Value); ``` ##### CPP File ```cpp= void AExampleCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent; PlayerInputComponent->BindAxis("MoveForward", this, &AExampleCharacter::MoveForward); PlayerInputComponent->BindAxis("MoveRight", this, &AExampleCharacter::MoverRight); PlayerInputComponent->BindAxis("Turn", this, &AExampleCharacter::Turn); PlayerInputComponent->BindAxis("LookUp", this, &AExampleCharacter::LookUp); PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump); } void AExampleCharacter::MoveForward(float Value) { if (Controller != nullptr && Value != 0.f) { const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f); const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X)); AddMovementInput(Direction, Value); } } void AExampleCharacter::MoveRight(float Value) { if (Controller != nullptr && Value != 0.f) { const FRotator YawRotation(0.f, Controller->GetControlRotation().Yaw, 0.f); const FVector Direction(FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y)); AddMovementInput(Direction, Value); } } void AExampleCharacter::Turn(float Value) { AddControllerYawInput(Value); } void AExampleCharacter::LookUp(float Value) { AddControllerPitchInput(Value); } ``` --- ### C++ AnimInstance Class #### Animation Blueprint ##### Header File ```cpp= public: virtual void NativeInitializeAnimation() override; virtual void NativeUpdateAnimation(float DeltaTime) override; private: UPROPERTY(BlueprintReadyOnly, Catergory = Character, meta = (AllowPrivateAccess = "true")) class AExampleCharacter* ExampleCharacter; UPROPERTY(BlueprintReadyOnly, Catergory = Movement, meta = (AllowPrivateAccess = "true")) float Speed; UPROPERTY(BlueprintReadyOnly, Catergory = Movement, meta = (AllowPrivateAccess = "true")) bool bIsInAir; UPROPERTY(BlueprintReadyOnly, Catergory = Movement, meta = (AllowPrivateAccess = "true")) bool bIsAccelerating; ``` ##### CPP File ```cpp= #include "ExampleCharacter.h" #include "GameFramework/CharacterMovementComponent.h" ``` ```cpp= void UExampleAnimInstance::NativeInitializeAnimation() { Super::NativeInitializeAnimation(); ExampleCharacter = Cast<AExampleCharacter>(TryGetPawnOwner()); } void UExampleAnimInstance::NativeUpdateAnimation(float DeltaTime) { Spuer::NativeUpdateAnimation(DeltaTime); if (ExampleCharacter == nullptr) { ExampleCharacter = Cast<AExampleCharacter>(TryGetPawnOwner()); } if (ExampleCharacter == nullptr) return; FVector Velocity = ExampleCharacter->GetVelocity(); Velocity.Z = 0.f; Speed = Velocity.Size(); bIsInAir = ExampleCharacter->GetCharacterMovement()->IsFalling(); bIsAccelerating = ExampleCharacter->GetCharacterMovement() ->GetCurrentAcceleration().Size() > 0 ? true : false; } ``` --- ### C++ GameMode Class #### Lobby Game Mode ##### Header File ```cpp= public: virtual void PostLogin(APlayerController* NewPlayer) override; ``` ##### CPP File ```cpp= #include "GameFramework/GameStateBase.h" void ALobbyGameMode::PostLogin(APlayerController* NewPlayer) { Super::PostLogin(NewPlayer); int32 NumberOfPlayers = GameState.Get()->PlayerArray.Num(); if (NumberOfPlayers == 2) { UWorld* World = GetWorld(); if (World) { bUseSeamlessTravel = true; World->ServerTravel(FString("/Game/Maps/ExampleMap?listen")); } } } ``` --- ### C++ UserWidget Class #### Overhead Widget ##### Header File ```cpp= public: UPROPERTY(meta = (BindWidget)) class UTextBlock* DisplayText; void SetDisplayText(FString TextToDisplay); UPROPERTY(BlueprintCallable) void ShowPlayerNetRole(APawn* InPawn); protected: virtual void OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld) override; ``` ##### CPP File ```cpp= #include "Components/TextBlock.h" void UOverheadWidget::OnLevelRemovedFromWorld(ULevel* InLevel, UWorld* InWorld) { RemoveFromParent(); Super::OnLevelRemovedFromWorld(InLevel, InWorld); } void UOverheadWidget::SetDisplayText(FString TextToDisplay) { if (DisplayText) { DisplayText->SetText(FText::FromString(TextToDisplay)); } } void UOverheadWidget::ShowPlayerNetRole(APawn* InPawn) { ENetRole LocalRole = InPawn->GetLocalRole(); FString Role; switch(LocalRole) { case ENetRole::ROLE_Authority: Role = FString("Authority"); break; case ENetRole::ROLE_AutonomousProxy: Role = FString("Autonomous Proxy"); break; case ENetRole::ROLE_SimulatedProxy: Role = FString("Simulated Proxy"); break; case ENetRole::ROLE_None: Role = FString("None"); break; } FString LocalRoleString = FString::Printf(TEXT("Local Role: %s"), *Role); SetDisplayText(LocalRoleString); } ```