GameplayDebugger font size

FGameplayDebuggerCategory.DrawData() uses FGameplayDebuggerCanvasContext to draw debug data to the screen. It has pointer to UFont. How can I change the font size?

My implementation:

void FMyGameplayDebuggerCategory::DrawData(APlayerController* OwnerPC, FGameplayDebuggerCanvasContext& CanvasContext)
{
    // enabling shadow works
	CanvasContext.FontRenderInfo.bEnableShadow = true;
	
	// scaling factor makes no difference
	CanvasContext.Font->SetFontScalingFactor(3);

	// check font size
	float width; // is always 8 (at any scaling factor)
	float height; // is always 15 (at any scaling factor)
	CanvasContext.Font->GetCharSize(*"A", width, height);

	for (const FString& debugInfoString : DebugData.DebugStrings)
	{
		CanvasContext.Printf(*debugInfoString);
	}
}

GameplayDebugger category uses FGameplayDebuggerCanvasContext for drawing onto the screen. This class has a pointer to UFont used for the text and it’s initialized by the UGameplayLocalController when the context is created. Font used by the initialization is GEngine->GetSmallFont(). It’s hardcoded and AFAIK there’s no API to change it at runtime. I can see three ways to solve this.

First would be to edit the font file used by the small font. Second, is to change the hardcoded value to a larger font. Third way is to create a method inside FGameplayDebuggerCanvasContext to set the font at runtime.

No matter which method you choose, first you need a font file with the size you want. Fonts returned by the GEngine->GetTinyFont, GEngine->GetSmallFont, GEngine->GetMediumFont and GEngine->GetLargeFont are located in Engine Content → EngineFonts folder.

There isn’t really a large font to choose from. Each of the GEngine calls returns normal size font or a smaller one.

You can duplicate the font you want (I recommend Roboto) to any folder inside your Content folder. Open it and change font size. Open Project Settings → General Settings → Fonts and set the font. You can also do it by changing the DefaultEngine.ini (in your game repository):

Here I created Fonts/ in the Content folder and put my RobotoLarge font file there.

[/Script/Engine.Engine]
LargeFontName=/Game/Fonts/RobotoLarge.RobotoLarge

If you don’t want to edit engine code, you can set the small font to the one you created and it should work.


If you want the canvas context to use large font by default, change the font in UGameplayDebuggerLocalController::OnDebugDraw()

Replace:

FGameplayDebuggerCanvasContext CanvasContext(Canvas, GEngine->GetSmallFont());

with:

FGameplayDebuggerCanvasContext CanvasContext(Canvas, GEngine->GetLargeFont());

and recompile the engine. UBT should only recompile the GameplayDebugger module.


You can also create an API to change the font at runtime.

Add to the declaration of FGameplayDebuggerCanvasContext class:
void SetFont(UFont* font);

and the definition in the cpp file:

void FGameplayDebuggerCanvasContext::SetFont(UFont* font)
{
	Font = font;
}

and recompile the engine.

In your override of the FGameplayDebuggerCategory::DrawData() call CanvasContext.SetFont(GEngine->GetLargeFont());

Recompile the game. When you open GameplayDebugger, the header will still use the default font but your custom debug data should be in large font.

200346-ue4editor-win64-debug_2017-06-11_06-42-45.png


This applies when overriding Actor’s DisplayDebug() method.

You can change font used for displaying text on canvas by getting the pre-set font from UEngine class and setting it in FDisplayDebugManager. See example:

void MyActor::DisplayDebug(UCanvas * Canvas, const FDebugDisplayInfo & DebugDisplay, float & YL, float & YPos)
{
	FDisplayDebugManager& displayDebugManager = Canvas->DisplayDebugManager;
	UFont* largeFont = GEngine->GetLargeFont();
	displayDebugManager.SetFont(largeFont);
	displayDebugManager.DrawString(TEXT("Text written with large font"));
}

Create your own font file and set in the Project Settings as explained above.
Restart editor. Run the game, FDisplayDebugManager should now use your custom font.