14-07-2021

Lua provides a registry, a pre-defined table that can be used by any C code to store whatever Lua value it needs to store. This table is always located at pseudo-index LUAREGISTRYINDEX. Any C library can store data into this table, but it should take care to choose keys different from those used by other libraries, to avoid collisions. The C/Invoke Lua Binding uses the new Lua module system, so to use C/Invoke in your Lua programs, load the module like so: require ('cinvokelua') To call C functions easily without writing a Lua module, declare a new instance of the clibrary class to load a dynamic library. The clibrary instance has methods which allow you to load functions. Creating and using Modules. There are actually two ways to make modules, the new way for 5.1 and 5.2 and the old (and deprecated) way for 5.0 and early 5.1. We start by covering the new way followed by a discussion on old way for Lua 5.0 and 5.1. Create an example file mymodule.lua with the following content.

Lua Load Library

The runtime Lua environment hosted by the Stingray engine is powered by the LuaJIT library. LuaJIT offers a very fast script interpreter and just-in-time compiler, in addition to several extensions to the base Lua API. One of these additional modules is its FFI library, which aims to allow easy access to C functions and data structures from Lua scripts without requiring any custom bindings on the C side.

If you have C code in a dynamically linked library, you should be able to take advantage of the FFI library to access that code from your project's Lua scripts.

Note: If you are new to LuaJIT FFI, see this page for an introduction to the concepts and syntax involved.

This page shows a simple example of how to set this up. For more complex examples, use the information provided in the FFI tutorial, FFI API and FFI semantics pages in the LuaJIT documentation.

When you use the FFI library to access C code, you do not have to write any special binding code. Your code should be plain C. For example, the following code declares a function that calls the Windows operating system to display a dialog box:

When you compile this code into a library, there is no need to link your C library expressly against LuaJIT, since your code has no dependency on Lua at all.

If you want to use functions from a third-party library instead of writing your own C code, you can usually use the library as-is as long as it has been compiled for the right processor architecture.

Lua load library

To invoke your C code from within your project's Lua scripts:

  1. Load the LuaJIT FFI library by calling require('ffi').

  2. You need to provide LuaJIT with some knowledge about the C data structures, variables and functions that you will be calling. To do this, you declare them in your Lua scripts, using standard C syntax, in a string that you pass to the ffi.cdef function.

  3. Load your C library by calling the ffi.load() function. Unlike an extension library that contains bindings between Lua and C, you do not use the require function.

  4. Call your C code.

Lua Load Library

For example:

Lua String Library

If you are using the Appkit in your game, you could put this code in your script/lua/project.lua file in order to make it run when the game is started.

Note that after you do steps 1-3 to bring the C constructs into the Lua environment, you can then invoke the functions at any time you need them. So, you can do steps 1-3 once during your game's initialization, then call the functions from your scripts or from custom Flow nodes.

File

Lua Load C Library Download

See Packaging a dynamically linked library.