C++ Lua Interface

  • Lua (/ ˈ l uː ə / LOO-ə; from Portuguese: lua meaning moon) is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications. Lua is cross-platform, since the interpreter of compiled bytecode is written in ANSI C, and Lua has a relatively simple C API to embed it into applications.
  • Finally, Lua-Terra can also be embedded in pre-existing applications by linking the application against libterra.a and using Terra’s C API. The interface is very similar to that of the Lua interpreter. A simple example initializes Terra and then runs code from the file specified in each argument.

This first edition was written for Lua 5.0. /mustek-gsmart-mini-3-drivers-for-mac-15266/. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.

C++ Lua Interface
Programming in Lua
Part IV. The C APIChapter 24. An Overview of the C API

The name of the function is taken from the arguments and the Lua function is placed on top of the stack of the Lua virtual machine. The rest of the arguments are placed on the Lua stack using the ReadLuaValue function; The Lua function is executed and the result (or error) is send back to the JavaScript. Tables are everything. There is no Lua without tables. Here is a brief introduction on how to interface with Lua tables using the C API. Code: luanewtable (ls); // creates an empty table on top of the stack. Luapushstring (ls, 'tablekey' ); // pushes a value onto the stack that we will use as a table key. Modern C Lua Interface. As we continue to grow the Lua scripting API for Vortex, it’s become clear that we need a better, less verbose way to expose “classes” and their functions to the Lua VM. In Vortex we decided to build our custom Lua binding.

24.1 – A First Example

C++ Lua Interface Tutorial

We will start this overview with a simple example of anapplication program:a stand-alone Lua interpreter.We can write a primitive stand-alone interpreter as follows:

WARNING: this code is for Lua 5.0.To run it in Lua 5.1,you must change the five calls luaopen_*(L)to a single call to luaL_openlibs(L).The header file lua.h defines the basic functions provided by Lua.That includes functions to create a new Lua environment(such as lua_open),to invoke Lua functions (such as lua_pcall),to read and write global variables in the Lua environment,to register new functions to be called by Lua, and so on.Everything defined in lua.h has the lua_ prefix.

The header file lauxlib.h defines the functions providedby the auxiliary library (auxlib).All its definitions start with luaL_(e.g., luaL_loadbuffer).The auxiliary library uses the basic API provided by lua.hto provide a higher abstraction level;all Lua standard libraries use the auxlib.The basic API strives for economy and orthogonality,whereas auxlib strives for practicality for common tasks.Of course, it is very easy for your program to create otherabstractions that it needs, too.Keep in mind that the auxlib has no access to the internals of Lua.It does its entire job through the official basic API.

The Lua library defines no global variables at all.It keeps all its state in the dynamic structure lua_Stateand a pointer to this structure is passed as an argumentto all functions inside Lua.This implementation makes Lua reentrantand ready to be used in multithreaded code.

The lua_open function creates a new environment (or state).When lua_open creates a fresh environment,this environment contains no predefined functions,not even print.To keep Lua small, all standard librariesare provided as separate packages,so that you do not have to use them if you do not need to.The header file lualib.h defines functions to open the libraries.The call to luaopen_io, for instance, creates the iotable and registers the I/O functions(io.read, io.write, etc.) inside it.

After creating a state and populating it with the standard libraries,it is time to interpret the user input.For each line the user enters,the program first calls luaL_loadbufferto compile the code.If there are no errors, the call returns zeroand pushes the resulting chunk on the stack.(Remember that we will discuss this 'magic' stack indetail in the next section.)Then the program calls lua_pcall,which pops the chunk from the stack and runs it in protected mode.Like luaL_loadbuffer,lua_pcall returns zero if there are no errors.In case of errors,both functions push an error message on the stack;we get this message with lua_tostring and,after printing it, we remove it from the stackwith lua_pop.

Notice that, in case of errors,this program simply prints the error message to the standard error stream.Real error handling can be quite complex in Cand how to do it depends on the nature of your application.The Lua core never writes anything directly to any output stream;it signals errors by returning error codes and error messages.Each application can handle these signals in away most appropriate for its needs.To simplify our discussions,we will assume for now a simpleerror handler like the following one,which prints an error message,closes the Lua state, and exits from the whole application:Later we will discuss more about error handling in theapplication code.

Because you can compile Lua both as C and as C++ code,lua.h does not include this typical adjustment codethat is present in several other C libraries:Therefore, if you have compiled Lua as C code(the most common case)and are using it in C++,you must include lua.h as follows:A common trick is to create a header file lua.hppwith the above code and to include this new file in yourC++ programs. Mw2 free mod menu.

Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.

Doggynub

LUA / C++

C++ Lua Interface Examples

okay i need to have this table in lua

I want to make it like that with the lua api
i have a std:: map like that :
[cpp]
std::map<std::string,int> status;
status['equip'] = int;
status['depot'] = int;
status['hold'] = int;
status['slot'] = int;
[/cpp]
Now in the c++ this supposed to be a argument in a function in creatureevent, so how to make it the proper way i have it like that
[cpp]
lua_newtable(L);
LuaInterface::setField(L,'equip',(int32_t)status['equip']);
LuaInterface::setField(L,'hold', (int32_t)status['hold']);
LuaInterface::setField(L,'depot',(int32_t)status['depot']);
LuaInterface::setField(L,'slot', (int32_t)status['slot']);
[/cpp]
and i get that error in console when the event supposed to be executed