Why does my HUD crash the Editor when pressing Play In Editor?

Following my previous question, on getting the light levels at the player’s position, I ran into another problem. I want to draw a string in my HUD class to display the results of the visibility function. Unfortunately, when I play in the editor to see the code in action, it crashes. What eludes me is that I do almost the same thing in the tick function of my player character class, except I call AddOnScreenDebugMessage() instead.

Here’s the HUD code:

void AAdNoctvmHUD::DrawHUD()
	FString Visibility = TEXT("Visibility: ") + FString::SanitizeFloat(Player->GetLightingAmount());
	DrawText(Visibility, FLinearColor::White, Canvas->ClipX / 2, Canvas->ClipY - 30, HUDFont, 1.0f, false);

Player tick code:

void AAdNoctvmPlayerCharacter::Tick(float DeltaSeconds)
	FString Visibility = TEXT("Visibility: ") + FString::SanitizeFloat(GetLightingAmount());
	GEngine->AddOnScreenDebugMessage(-1, 0.1f, FColor::White, Visibility);

And the crash log: !Name:VibgyorAccess violation - code c0000005 (first/second chance not avail - Pastebin.com

Hi vibgyorc6,

I tried replicating the issue you described using your code, but was not able to get the Editor to crash. What version of the Editor are you using, and are you using an Editor compiled from source code or the binary version from the Launcher?

Also, could you specify where and how the variable HUDFont is being declared in your HUD class?

One final thought for now: Have you tried checking for a valid reference to GEngine?

if (GEngine)
		GEngine->AddOnScreenDebugMessage(-1, 0.1f, FColor::White, Visibility);

I’m using the 4.2 editor compiled from source. HUDFont is being declared like this.


UFont* HUDFont;


AAdNoctvmHUD::AAdNoctvmHUD(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
	static ConstructorHelpers::FObjectFinder<UFont> HUDFontOb(TEXT("/Engine/EngineFonts/RobotoDistanceField"));
	HUDFont = HUDFontOb.Object;


For your last question, I have not, but I will try checking all the pointers I’m referencing.

I figured out the problem. Turns out, I’m not supposed to cast my player class in the constructor. :stuck_out_tongue: Instead, I’m now casting it in DrawHUD() and it’s working.