July 2018
Beginner
202 pages
5h 42m
English
Because functions in C and Lua work so differently, exposing a C function to Lua can get a bit tricky. All C functions that Lua can call must follow the signature of lua_CFunction, which is defined in lua.h as the following:
typedef int (*lua_CFunction) (lua_State *L);
This function takes only one argument, the lua_State. The return value of the function is an integer. This integer is the number of elements that the function pushed onto the stack as return values.
Let's take for example a simple C function that returns the magnitude of a three-dimensional vector. In C, the code for doing so might look ...