[SOLVED] Can I use external javascript libaries too?

I know that in the armory 3d projects can use external hexe libaries but I interested, possible to use in
javascript or other prog language written external libraries?


Hi, yes this is possible, but it depends on the target platform. If you target JS (e.g. HTML5 or Krom) you can use JS libraries or even WebAssembly, on static targets such as HL (HashLink) you can use C/C++ libraries or some dynamically linked libraries written in other languages. If you create a custom Krom binary you can even use C/C++/dynamically linked libraries from Javascript, if you also create the necessary JS API.

How you actually integrate those libraries also depends on the target platform. Haxe provides you with various tools to inject or link to external code, and for specific questions in that regard you should better consult the Haxe forums.

I don’t know how much knowledge you already have about Armory and especially its build system (or better: Kha’s build system), feel free to ask if below explanations aren’t helpful enough :slight_smile:

Targeting JS

To load a library on HTML5 you probably want to include it from the html file via <script>, which Armory unfortunately makes a bit difficult because it re-generates that file every time you build the game without giving you a chance to modify it. But you can work around this issue by adding a postBuild callback to your khafile.js that swaps the html file with a custom version after the build is completed (example). Note that Armory also generates the khafile automatically, for that you can use Armory Project > Modules > Append Khafile to append a Blender textblock to the end of the khafile.

Loading a library on Krom is a bit more cumbersome because there is no html file which you could modify. Instead you probably need to load the .js file as an asset (using Kha’s asset API) and then evaluate it like in this example. It might also be possible to modify the DOM and programmatically add a script tag, but I’m not sure about this.

To then call the JS library, you can use js.Syntax.code() which is handy when you only need to call a few individual functions, or for larger libraries you can write externs for which you can find a (rather complex) example here. Haxe externs will automatically translate to the correct target language.

There are probably more ways of Haxe/JS interoperation (e.g. WebAssembly which is not explained in my post), but the above should give you a good overview.

Targeting HL

Since you mainly asked about Javascript I will keep the following explanation a shorter, but again, don’t hesitate to ask if you have more questions :slight_smile:

In a khafile.js, you can load Kha libraries via project.addLibrary() or project.addSubproject(). However, you can also load Kinc libraries this way (Kinc is basically Kha’s backend written in C/C++), which allow you to add C/C++ headers and source files to your project (example). You can then create Hashlink externs (which work slightly different than JS externs) for your custom C/C++ files, those files can then call code from the library.

For Kha’s hxcpp target (which Armory doesn’t use) this would work pretty similar by the way.

1 Like