Calling C code from Lua

You can make your project's Lua scripts execute C code. This can be useful for times where performance is key, since a function written in C will typically run faster than the equivalent function in Lua. In addition, you might be able to use this approach to integrate third-party libraries or re-use existing C code written for a different application.

To do this, you have a few options:

In all cases, you need to compile your C code into a dynamically linked library, and make that library available to the engine for loading at runtime.

Note: The material in this section is recommended for programmers with prior experience writing and compiling C code. C can be difficult for beginners to get used to on its own, even without the added complexity of managing the binding layer or the type conversions involved in using the FFI library.

Platform support

You can only run C code in a dynamically linked library if your game targets a platform that supports dynamic linking and that links dynamically to LuaJIT.

Another option: writing an engine plug-in

If you need to run C code on a platform that doesn't support dynamic linking, or if you can't get your code to work from Lua using either of the approaches described in the topics in this section, your other option is to use the plug-in API exposed by the Stingray engine.

This currently requires access to the Stingray source repositories; contact an Autodesk representative for details. See also the Stingray Developer Help.