On part 17 of the Intro to UE4 Programming: Coding a canvas HUD. Finished the vid, and was able to compile everything w/o error, but when I run the editor and click play, nothing shows up on the screen.
TutorialCodeHUD.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/HUD.h"
#include "TutorialCodeHUD.generated.h"
/**
*
*/
UCLASS()
class TUTORIALCODE_API ATutorialCodeHUD : public AHUD
{
GENERATED_BODY()
public:
/* Variable for storing the font */
UPROPERTY()
UFont* HUDFont;
ATutorialCodeHUD::ATutorialCodeHUD(FObjectInitializer&);
/* Primary draw call for the HUD */
virtual void DrawHUD() override;
};
TutorialCodeHUD.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "TutorialCode.h"
#include "TutorialCodeHUD.h"
#include "TutorialCodeGameMode.h"
#include "TutorialCodeCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "Engine/Canvas.h"
#include "Engine/Font.h"
ATutorialCodeHUD::ATutorialCodeHUD(FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// Use the RobotoDistanceField font from the engine
static ConstructorHelpers::FObjectFinder<UFont> HUDFontOb(TEXT("/Engine/Content/EngineFonts/RobotoDistanceField"));
HUDFont = HUDFontOb.Object;
}
void ATutorialCodeHUD::DrawHUD()
{
// Get screen dimensions
FVector2D ScreenDimensions = FVector2D(Canvas->SizeX, Canvas->SizeY);
// Call to the parent versions of DrawHUD
Super::DrawHUD();
// Get the character and print it's power level
ATutorialCodeCharacter* MyCharacter = Cast<ATutorialCodeCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
FString PowerLevelString = FString::Printf(TEXT("%10.1"), FMath::Abs(MyCharacter->PowerLevel));
DrawText(PowerLevelString, FColor::White, 50, 50, HUDFont);
ATutorialCodeGameMode* MyGameMode = Cast<ATutorialCodeGameMode>(UGameplayStatics::GetGameMode(this));
// If this game is over
if (MyGameMode->GetCurrentState() == ETutorialCodePlayState::EGameOver)
{
// Create a variable for storing the size of print Game Over
FVector2D GameOverSize;
GetTextSize(TEXT("GAME OVER"), GameOverSize.X, GameOverSize.Y, HUDFont);
DrawText(TEXT("GAME OVER"), FColor::White, (ScreenDimensions.X - GameOverSize.X) / 2.0f, (ScreenDimensions.Y - GameOverSize.Y) / 2.0f, HUDFont);
}
}
TutorialCodeGameMode.h
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "SpawnVolume.h"
#include "GameFramework/GameMode.h"
#include "TutorialCodeGameMode.generated.h"
// enum to store the current state of Game play
enum class ETutorialCodePlayState : uint8
{
EPlaying,
EGameOver,
EUnknown
};
UCLASS(minimalapi)
class ATutorialCodeGameMode : public AGameMode
{
GENERATED_BODY()
virtual void Tick(float DeltaSeconds) override;
ETutorialCodePlayState CurrentState;
TArray<ASpawnVolume*> SpawnVolumeActors;
void HandleNewState(ETutorialCodePlayState NewState);
public:
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Power)
float DecayRate;
ATutorialCodeGameMode(const FObjectInitializer& ObjectInitializer);
virtual void BeginPlay() override;
void SetCurrentState(ETutorialCodePlayState NewState);
FORCEINLINE ETutorialCodePlayState GetCurrentState() const { return CurrentState; }
};
TutorialCodeGameMode.cpp
// Copyright 1998-2014 Epic Games, Inc. All Rights Reserved.
#include "TutorialCode.h"
#include "TutorialCodeGameMode.h"
#include "TutorialCodeCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "TutorialCodeHUD.h"
ATutorialCodeGameMode::ATutorialCodeGameMode(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
// set default pawn class to our Blueprinted character
static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/Blueprints/MyCharacter"));
if (PlayerPawnBPClass.Class != NULL)
{
DefaultPawnClass = PlayerPawnBPClass.Class;
}
// Set the default HUD class
HUDClass = ATutorialCodeHUD::StaticClass();
// Set base decay rate
DecayRate = 0.25f;
}
void ATutorialCodeGameMode::Tick(float DeltaSeconds)
{
// get the player character (pawn)
ATutorialCodeCharacter* MyCharacter = Cast<ATutorialCodeCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
// If the character still has power
if (MyCharacter->PowerLevel > 0.05)
{
// Decrease the character's power
MyCharacter->PowerLevel = FMath::FInterpTo(MyCharacter->PowerLevel, 0.f, DeltaSeconds, DecayRate);
}
else
{
SetCurrentState(ETutorialCodePlayState::EGameOver);
}
}
void ATutorialCodeGameMode::SetCurrentState(ETutorialCodePlayState NewState)
{
CurrentState = NewState;
HandleNewState(NewState);
}
void ATutorialCodeGameMode::HandleNewState(ETutorialCodePlayState NewState)
{
switch (NewState)
{
// When we're playing, the spawn volumes can spawn
case ETutorialCodePlayState::EPlaying:
for (ASpawnVolume* Volume : SpawnVolumeActors)
{
Volume->EnableSpawning();
}
break;
// if the game is over, the spawn volumes should deactivate
case ETutorialCodePlayState::EGameOver:
{
for (ASpawnVolume* Volume : SpawnVolumeActors)
{
Volume->DisableSpawning();
}
APlayerController* PlayerController = UGameplayStatics::GetPlayerController(this, 0);
PlayerController->SetCinematicMode(true, true, true);
}
break;
case ETutorialCodePlayState::EUnknown:
default:
// Do nothing
break;
}
}
void ATutorialCodeGameMode::BeginPlay()
{
Super::BeginPlay();
// Find all spawn volume actors
TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ASpawnVolume::StaticClass(), FoundActors);
for (auto Actor : FoundActors)
{
ASpawnVolume* SpawnVolumeActor = Cast<ASpawnVolume>(Actor);
if (SpawnVolumeActor)
{
SpawnVolumeActors.Add(SpawnVolumeActor);
}
}
SetCurrentState(ETutorialCodePlayState::EPlaying);
}