Hi, here is my HUD class for my FPS game that I made, following on from the FPS tutorial.
Everything here works fine except for the text, where it flashes grey whenever I click, but otherwise does not show. Same thing happens with FCanvasTextItem.
#include "FPSProject.h"
#include "FPSHUD.h"
#include "FPSCharacter.h"
AFPSHUD::AFPSHUD(const class FObjectInitializer& PCIP) : Super(PCIP) {
// Load the default font
ConstructorHelpers::FObjectFinder<UFont> FontObject(TEXT("Font'/Game/Fonts/verdana.Verdana'"));
if (FontObject.Object) {
defaultTextFont = FontObject.Object;
}
// Set the crosshair texture
static ConstructorHelpers::FObjectFinder<UTexture2D> CrosshairTexObj(TEXT("Texture2D'/Game/Crosshair.Crosshair'"));
CrosshairTex = CrosshairTexObj.Object;
static ConstructorHelpers::FObjectFinder<UTexture2D> BulletTexObj(TEXT("Texture2D'/Game/Images/Bullet.Bullet'"));
BulletTex = BulletTexObj.Object;
static ConstructorHelpers::FObjectFinder<UTexture2D> BulletGreyTexObj(TEXT("Texture2D'/Game/Images/BulletGrey.BulletGrey'"));
BulletGreyTex = BulletGreyTexObj.Object;
}
void AFPSHUD::DrawHUD() {
Super::DrawHUD();
// Draw very simple crosshair
// Find center of the Canvas
const FVector2D Center(Canvas->ClipX * 0.5f, Canvas->ClipY * 0.5f);
// Offset by half the texture's dimensions so that the center of the texture aligns with the center of the Canvas
const FVector2D CrosshairDrawPosition((Center.X - (CrosshairTex->GetSurfaceWidth() * 0.5)), (Center.Y - (CrosshairTex->GetSurfaceHeight() * 0.5f)));
// Draw the crosshair
FCanvasTileItem CrosshairTileItem(CrosshairDrawPosition, CrosshairTex->Resource, FLinearColor::White);
CrosshairTileItem.BlendMode = SE_BLEND_Translucent;
Canvas->DrawItem(CrosshairTileItem);
if (GetWorld()->GetName() == "FPSMap") {
uint32 displayAmmo = 10;
if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 9) {
displayAmmo = 10;
} else if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 8) {
displayAmmo = 9;
} else if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 7) {
displayAmmo = 8;
} else if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 6) {
displayAmmo = 7;
} else if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 5) {
displayAmmo = 6;
} else if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 4) {
displayAmmo = 5;
} else if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 3) {
displayAmmo = 4;
} else if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 2) {
displayAmmo = 3;
} else if (AFPSCharacter::currentAmmo > (AFPSCharacter::clipSize / 10) * 1) {
displayAmmo = 2;
} else if (AFPSCharacter::currentAmmo > 0) {
displayAmmo = 1;
} else {
displayAmmo = 0;
}
for (uint32 i = 0; i < displayAmmo; i++) {
const FVector2D BulletDrawPosition((Canvas->ClipX - BulletTex->GetSurfaceWidth() - (12.f * i)), (Canvas->ClipY - BulletTex->GetSurfaceHeight() - 25));
FCanvasTileItem BulletTileItem(BulletDrawPosition, BulletTex->Resource, FLinearColor::White);
BulletTileItem.BlendMode = SE_BLEND_Translucent;
Canvas->DrawItem(BulletTileItem);
}
for (uint32 i = displayAmmo; i < 10; i++) {
const FVector2D BulletDrawPosition((Canvas->ClipX - BulletGreyTex->GetSurfaceWidth() - (10.f * i)), (Canvas->ClipY - BulletGreyTex->GetSurfaceHeight() - 25));
FCanvasTileItem BulletTileItem(BulletDrawPosition, BulletGreyTex->Resource, FLinearColor::White);
BulletTileItem.BlendMode = SE_BLEND_Translucent;
Canvas->DrawItem(BulletTileItem);
}
uint32 startTextPos = Canvas->ClipX - BulletTex->GetSurfaceWidth() - 100.f - 20.f;
const FVector2D AmmoDrawPosition(30, (Canvas->ClipY - BulletTex->GetSurfaceHeight() - 15.f));
FFontRenderInfo RenderInfo = Canvas->CreateFontRenderInfo(true, false, FColor::Green, AmmoDrawPosition);
Canvas->DrawText(defaultTextFont, FText::FromString("" + AFPSCharacter::currentAmmo), AmmoDrawPosition.X, AmmoDrawPosition.Y, 1.f, 1.f, RenderInfo);
}
}