Lua Invoke C Function

  1. C Function Tutorial
  2. Lua C Function
  3. Lua Calling C Functions
Lua Invoke C Function

This article will present an introductory example of calling lua functions from C++.


If you want to extend and customize the capabilities of a C++ applicationwithout requiring a full recompilation, then using a embedded scripting languageis the way to go. Lua is one such embeddable scripting language, and is very popularamong game developers. The main advantage of Lua, in my opinion, is that the core APIis very minimal, has very small memory footprint. The availability of LuaJIT makesit a very performant alternative as well.

This article is a continuation of the earlier introductory article Minimal C++ Example < filename minimal-lua.rst>.In this article we will discuss how to call lua functions from C++.

Code Explained

Lua

The initial part of the code initialises the lua_State loads and executes the script with lua_pcall(L,0,0,0).Once the script is loaded, the functions are available in the global namespace. Here the script basically is a functionthat takes two numbers and returns the sum of the two. Using the lua_getglobal(L, 'sum') call,we load the function into the stack. We can check if the function was loaded correctly using the lua_isfunction(L, -1).Then we pass the two arguments of the function by pushing them into the stack. Then the lua_pcall method executes thefunction and loads the result onto the stack. The successful execution of the function can be checked by checkingthat the stack is not nil using !lua_isnil(L,-1). The returned value can then be accessed by casting the resultin the top of the stack using lua_tonumber(L,-1). We use lua_pop to clear the result from the stack.

Running this example should print:

After this call, the Lua VM will place the rest of the arguments in the register table, and then call the function we returned from the metaIndex method, which will again call into C, and land in the invoker function: int methodInvoker(luaState. L). Aug 19, 2012 This new tutorial explains how to call C functions from Lua 5.2, including passing and receiving arguments from them. These notes are based on the Lua-User Wiki Sample Code and the Lua 5.2 Reference Manual, as usual, and on the tutorial by Christian Stigen Larsen. First, I will show you some code (in Lua and C) and the execution output. Jul 26, 2014 Meaning you not only can call C from Lua but you can call Lua from C. Often you’ll see applications written in a high level language with specific functionally written in C. Typically this is performance critical code where the optimization is using C instead of the high level language. With Lua you can do this but also the opposite.

  • This new tutorial explains how to call C functions from Lua 5.2, including passing and receiving arguments from them. These notes are based on the Lua-User Wiki Sample Code and the Lua 5.2 Reference Manual, as usual, and on the tutorial by Christian Stigen Larsen. First, I will show you some code (in Lua and C) and the execution output.
  • To call a function in the global namespace (as mentioned by @THC4k) is easily done, and does not require loadstring. X='foo' G x - calls foo from the global namespace. You would need to use loadstring (or walk each table) if the function in another table, such as if x='math.sqrt'. If loadstring is used you would want to not only.

on the screen.

Conclusion

This article gave a very minimal example explaining how to call a lua function from C++.


lua

C Function Tutorial

programmingC++

Lua C Function


Lua Calling C Functions

Related Post