x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

C++ HUD Not Drawing

I am going through the video tutorials found here: https://wiki.unrealengine.com/Introduction_to_UE4_Programming_-_1_-_Overview to learn how c++ can be incorporated with blueprints. I ran into a snag at the very end of the series, on drawing the HUD that shows the score. Below is some of my relevant code: GameMode Initialization:

 ABatteryGameGameMode::ABatteryGameGameMode()
 {
     // set default pawn class to our Blueprinted character
     static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPerson/Blueprints/ThirdPersonCharacter"));
     if (PlayerPawnBPClass.Class != NULL)
     {
         DefaultPawnClass = PlayerPawnBPClass.Class;
     }
 
     HUDClass = ABatteryGameHUD::StaticClass();
 
     DecayRate = 0.125f;
 }
 

And my DrawHUD function:

 void ABatteryGameHUD::DrawHUD()
 {
     UE_LOG(LogTemp, Warning, TEXT("DrawHUD called"));
     FVector2D ScreenDimensions = FVector2D(Canvas->SizeX, Canvas->SizeY);
 
     Super::DrawHUD();
 
     ABatteryGameCharacter* MyCharacter = Cast<ABatteryGameCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
     FString PowerLevelString = FString::Printf(TEXT("%10.1"), FMath::Abs(MyCharacter->PowerLevel));
 
     DrawText(PowerLevelString, FColor::White, 50, 50, HUDFont);
 
     ABatteryGameGameMode* MyGameMode = Cast<ABatteryGameGameMode>(UGameplayStatics::GetGameMode(this));
 
     if (MyGameMode->GetCurrentState() == EBatteryGamePlayState::EGameOver)
     {
         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);
     }
 }


But when I play the game, the logging at the top of my DrawHUD function never shows up, so I know this function isn't being called. Finally, here is a screen shot of my Default Modes section of the Project Settings. According to this, the correct class is specified as the HUD class for my project. alt text Let me know if any more information would be helpful to diagnosing the problem.

Product Version: UE 4.8
Tags:
capture.png (16.8 kB)
more ▼

asked Jun 16 '15 at 04:12 PM in C++ Programming

avatar image

sigi0073
0 3 5 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

So I tried out your code:

My Code:

y TestHud.h // Fill out your copyright notice in the Description page of Project Settings.

 #pragma once
 
 #include "GameFramework/HUD.h"
 #include "TestHud.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class ENVIRONMENTWINDOW_API ATestHud : public AHUD
 {
     GENERATED_BODY()
     
 public:
     ATestHud(const FObjectInitializer& ObjectInitializer);
 
 
     UPROPERTY()
         UFont* HUDFont;
 
     /*primary draw call for the HUD*/
     virtual void DrawHUD() override;
     
     
 };


my TestHud.cpp // Fill out your copyright notice in the Description page of Project Settings.

 #include "EnvironmentWindow.h"
 #include "TestHud.h"
 
 ATestHud::ATestHud(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
 {
 
 }
 
 
 void ATestHud::DrawHUD()
 {
     UE_LOG(LogTemp, Warning, TEXT("DrawHUD called"));
     FVector2D ScreenDimensions = FVector2D(Canvas->SizeX, Canvas->SizeY);
 
     Super::DrawHUD();
 
     //ABatteryGameCharacter* MyCharacter = Cast<ABatteryGameCharacter>(UGameplayStatics::GetPlayerPawn(this, 0));
     FString PowerLevelString = FString::Printf(TEXT("%10.1"), FMath::Abs(10));//MyCharacter->PowerLevel));
 
     DrawText(PowerLevelString, FColor::White, 50, 50, HUDFont);
 
     //ABatteryGameGameMode* MyGameMode = Cast<ABatteryGameGameMode>(UGameplayStatics::GetGameMode(this));
 
 //     if (MyGameMode->GetCurrentState() == EBatteryGamePlayState::EGameOver)
 //     {
 //         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);
 //     }
 }

And this works for me (I commented out lines which referred to classes that I did not have). I get a log spammed with:

 LogTemp:Warning: DrawHUD called
 LogTemp:Warning: DrawHUD called
 LogTemp:Warning: DrawHUD called
 LogTemp:Warning: DrawHUD called


However, though since ATestHud::HudFont was never set, nothing is drawn.

Suggestion:

heck that you've not overridden the gamemode in world settings: alt text

world_settings.jpg (62.2 kB)
more ▼

answered Jul 16 '15 at 08:08 PM

avatar image

BunniPhD
136 4 68 15

avatar image sigi0073 Jul 16 '15 at 11:13 PM

Huh, I hadn't looked at this project in a while, but it turns out that I am getting exactly what you described. I am currently learning how to use the Unreal Engine, and started this project after experimenting with Blueprints. The Blueprint "Print" function prints to the screen as well as the log, and I had assumed the same would happen with UE_LOG(), which is why I wasn't seeing that debug notice popping up. Thanks for letting me know about setting the font, once I did that it is now showing up as my HUD.

avatar image umitcel Jan 23 '16 at 04:40 PM

Actually, adding constructor solved my problem, greetings.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Hello, sigi0073

Sorry to hear about your problem. This behavior may occur in the situation when override specifier hasn't been used in the place of DrawHUD() function declaration. Thus, please make sure that you DrawHUD() function is declared like this:

 virtual void DrawHUD() override;

Hope this helped!

Good luck!

more ▼

answered Jun 18 '15 at 04:56 PM

avatar image

Andrii Melnyk
1.9k 60 20 112

avatar image sigi0073 Jun 18 '15 at 05:48 PM

In my header, DrawHUD() already has the override specifier. Here is the content of the header for my HUD class: // Fill out your copyright notice in the Description page of Project Settings.

 #pragma once
 
 #include "GameFramework/HUD.h"
 #include "BatteryGameHUD.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class BATTERYGAME_API ABatteryGameHUD : public AHUD
 {
     GENERATED_BODY()
 
 public:
     ABatteryGameHUD(const FObjectInitializer& ObjectInitializer);
 
 
     UPROPERTY()
         UFont* HUDFont;
 
     /*primary draw call for the HUD*/
     virtual void DrawHUD() override;    
     
 };
(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question