简体   繁体   中英

How to pass an object from c++ to Lua and opposite?

I have an object created in c++, I want to pass it to Lua, in Lua i need get that object and pass to c++.

in c++:

class Measure
{
  public:
     Measure();
  private:
    int value;
}

Void PassObjToLua(lua_State *L)
{
  Measure * m=new Measure(); // I need to pass this object to Lua

 // How to implement?
}

Void GetObjFromLua(lua_State *L)
{
  //I need to get Measure object from Lua. How to implement?
}

In Lua script.

Local measure=nil;
measure= PassObjToLua();
GetObjFromLua(measure);

What do i need to do?

As Marcus wrote, you need to use the Lua C API in some way. To do it, you can either use a C code generator, or a Lua wrapper. You have examples of both on the lua wiki site , as well as instructions on how to write your own Lua wrapper.

EDIT: To expand my answer a bit. You need to represent a C++ class type instance as a lua type instance. The 2 options that are most popular are: representing a C++ object as a lua table or a lua userdata with a metatable attached. If you choose to use a lua table, you have the options of filling the table with keys, that are mapped to CFunction s, or use a metatable just as with userdata. A metatable allows you to hook table/uservalue key accesses and process them in C++.

The best way I know of is the Sol library, which exposes a C++ interface.

Its userdata example shows how the library can be used to create tables linked to the C++ objects.

This answer might look like it's link-only , but you only really need to take a peek at how complicated the library is to realize that the question can't really be answered in one, simple answer. There are numerous caveats with regard to using the C API and wrapping it in C++ primitives.

I personally dislike the macro-based reflection-like binders and would recommend against using one, altough of course other people might have different opinions.

You can use Lua's light user data to do that. In that case, the object is managed by C++ :

 extern "C" int PassObjToLua(lua_State *L)
 {
    Measure * m=new Measure(); // I need to pass this object to Lua
    lua_pushlightuserdata (L, m);
    return 1;
 }

 extern "C" int GetObjFromLua(lua_State *L)
 {
    Measure *m = (Measure*)lua_touserdata (L, 1);
    return 0;
 }

To register your C functions in the Lua context :

 lua_pushcfunction (L, GetObjFromLua);
 lua_setglobal (L, "GetObjFromLua");

 lua_pushcfunction (L, PassObjToLua);
 lua_setglobal (L, "PassObjToLua");

Lualib has a C interface, not a C++ one -- so without further ado, you'd have to generate extern C functions that you can use with Lua.

The place here is far too short to give you a complete introduction on how to use C++ with lua, or evn how to extend a C program with Lua. I think it's safe to say you will find a tutorial that fits exactly your needs when you use google with lua C interface .

EDIT : Here's a screenshot of my google search. How much more ridiculous can we get?

Google搜索> lua c界面教程的屏幕截图<

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM