Skip to content

Managing Your Assets

RblSb edited this page Oct 31, 2019 · 20 revisions

Include your assets

To use assets in kha first you need to include them into your project (khafile.js) so khamake can find them during the build stage. By default the folder Assets is created and linked in khafile.js, but you can add other folders by adding project.addAssets('OtherAssetFolder/**'); in your khafile.js.

khamake

One of the responsibilities of khamake beside building your project, is to transform your assets into more optimize format depending on your target. So for example if you add a .wav in your assets folder and you want to build for windows or mac, khamake will generate a .ogg file. If you target html5 and add the --ffmpeg command (node Kha/make html5 --ffmpeg "/path/to/ffmpeg.exe need the .exe) it will generate a .mp4 or in flash a .mp3. The same analogy works for image files.

Expected formats

khamake expects the assets to be in this formats:

  • Images: png, jpg, hdr
  • Sounds: wav (sample rate: 44100Hz 16 bit)
  • Videos: mp4
  • Fonts: ttf

Any other format will not be changed and will be available in your code through Assets.blobs as Blob.

Loading your assets

Before you can use an asset you need to load it into memory, to do so you need to use one of the loading method under the class Assets. If you want to load everything you can call loadEverything, but its only recommended if you don't use many assets, otherwise you will fill your memory.

Using the assets in your code

To use your assets after they are loaded into memory, you just need to call Assets..nameOfAsset.

Add more assets

Every time you add more assets you need to call khamake again.

Assets names

The names shown in Assets are loaded using a macro and a json file in the "-resources" folder, so all assets names must respect variable/function naming rules.

Normally khamake will flatten all assets paths and dump everything in the root folder. This will also cause issues if you have multiple assets with the same name in different directories. The following won't work as khamake will complain about duplicate tiles.png.

/assets
    /map1
        tiles.png
    /map2
        tiles.png

To prevent this issue and keep the paths, add assets like this:

project.addAssets('assets/**', {
     nameBaseDir: 'assets',
     destination: '{dir}/{name}',
     name: '{dir}/{name}'
});

You can now access them with kha.Assets.images.map1_tiles or kha.Assets.images.map2_tiles The separator for subdirectories is an underscore _.

Resize images

project.addAssets('assets/**', { scale: 0.5 });
Clone this wiki locally