Learn Haxe

An easier way to learn how to use the Haxe core and related technologies.

Loading JSON in Kha

Tagged with: Kha

With some macro magic, Kha allows you to load assets asynchronously. Here's how that looks with images:

Assets.loadEverything(function()
{
  var playerImage = Assets.images.player;
}

If you have a player.png file in Assets/images, this snippet loads that data into the playerImage variable.

That works great with images, but what about JSON? Can I add monsters.json to Assets/data and load it with Assets.images.data.monsters?

Nope. This doesn't compile; if you check the Assets class documentation, you'll notice that it has blobs, fonts, images, sounds, videos. Of these, blobs looks promising, but the documentation for BlobList is non-existent.

Some of the wiki documentation mentions that:

Any other format [other than images, sounds, and vidos] will not be changed and will be available in your code through Assets.blobs as Bytes.

It turns out that you can load your data using Assets.blobs.monsters_json. This is a little unintuitive (no directory structure/naming?), but it works. It also gives you back Bytes; you can call .toString() on it to read it as text.

If we put this JSON into Assets/data/monsters.json:

{
    "names": [
        "slime", "bat", "spider"
    ]
}

We can use this code to get back names as an Array<String>:

Assets.loadEverything(function()
{
    var monsterNames = Json.parse(Assets.blobs.monsters_json.toString()).names;
}

Note that you don't need to keep filenames distinct across directories to avoid naming collisions. This Kha wiki page mentions that Kha packs them into a directory structure; so if you have Assets/images/red/player.png and Assets/images/blue/player.png, Kha will expose them as Assets.images.red.player and Assets.images.blue.player respectively. (Thanks to @sh-dave for clarifying this.)