How to set UFont?

I am looking into using the UCanvas object, and noticed the DrawText function, and saw the TextFont parameter, which in turn is a UFont, and for the life of me, I can’t figure out how to specify that font. I know that there isn’t a default font, and I know how to do it in BP, but I prefer C++.

Thankyou in advance :slight_smile:

Within your HUD class, create a UFont and allow it to be edited from within the editor -

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Font)
UFont* someFont;

and then create a font in the editor. You can then use this font in the blueprint and then in your C++ code.

Thankyou, but do you know how this would be done in C++?

Might be able to use a constructor loading method within your constructor:

ConstructorHelpersFObjectFinder<UFont> TheFontObject(TEXT("/Game/Fonts/theFont.theFont");

Not fully tested this with fonts though.

Hmm, ok. Thank you for your help!

I dont have anything to copy paste but use the ConstructorHelpers::FindObject and get the font that way.
If someone have not posted a snip for you i will do this when i get home from work.

Edit:
I see someone was fast :wink:

Yeah I would try it, but I am unable to.

This is how i get my font.

// Constructor
ConstructorHelpers::FObjectFinder<UFont> FontObject(TEXT("Font'/Game/MyProject/Roboto18.Roboto18'"));
if (FontObject.Object)
{
    	Font = FontObject.Object;
}
// Snip from header.
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "HUD Propertys")
    	UFont* Font;

Works like a treat :slight_smile:

If You just want to test then GEngine->GetSmallFont()

do you know how to turn ttf to ufont?