Skip to content
RblSb edited this page Sep 5, 2018 · 12 revisions

Using fonts in Kha

To use an external font in Kha you first need to load the font in question, like any other asset. Then after loading it you will be able to access it by calling Assets.fonts.myfontname considering that the name of your font is myfontname.ttf.

Loading the font

You will need to place the font file in the Assets folder and then load it in the project by doing this:

class Main {

	import kha.System;
	import kha.Window;
	import kha.Assets;
	import kha.Font;

	public static function main():Void {
		System.init(
			{title: "Project", width: 1024, height: 768},
			function (window:Window) {
				Assets.loadFont("Aclonica_Regular", whenImDone);
			}
		);
	}

	public static function whenImDone(f:Font):Void {
		new Project();
	}

}

The reason of loading at the start of the project is that you will probably need to access the font in your project and if you call the load just before the new of your project, you will encounter an error since the asset loading and the new calls are concurrent calls. The whenImDone function here solves the issue, and calls the start of the Project when the font is finished loading so you can use the font without faults in that project.

Other ways of loading a font

In Kha there is another way of loading assets in general. Basically you can do Assets.loadEverything(whenImDone) which is a valid solution when you have all your assets in the Assets folder but it is not advised to use this method with projects with a lot of assets since that can fill your RAM up.