Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding C++ methods and variables to a table, side by side
    primarykey
    data
    text
    <p>I've been attempting to create a "host" application that exposes a C++ API to Lua code, and have been fairly successful so far, but have run into snags when attempting to bind "variables" in with the methods.</p> <p>The pattern I have developed to bind "classes" to lua involves each class having a <code>_new</code> and <code>_gc</code> function, as well as a static <code>luaL_Reg RegData[]</code> for each class. I can then assign the desired functions into the RegData array, and call a helper method to bind them to lua. The following is some code illustrating my method:</p> <pre class="lang-c prettyprint-override"><code>int Host::_new(lua_State * ls) { Host ** udata = (Host **)lua_newuserdata(ls, sizeof(Host *); *udata = new Host(); luaL_getmetatable(ls, "luaL_Host); lua_setmetatable(ls, -2); return 1; } int Host::_gc(lua_State * ls) { Host * host = *(Host **)luaL_checkudata(ls, 1, "luaL_Host"); delete host; return 0; } const luaL_Reg Host::RegistrationData[] = { { "new" , Host::_new }, { "__gc" , Host::_gc }, { 0 , 0 } }; </code></pre> <p>And elsewhere:</p> <pre class="lang-c prettyprint-override"><code>void LuaState::registerObject(const char * name, const luaL_Reg data[]) { int len = strlen(name) + 6; char * luaLname = new char[len]; snprintf(luaLname, len, "luaL_%s", name); // create the metatable with the proper functions luaL_newmetatable(_state, luaLname); luaL_setfuncs(_state, data, 0); // copy the metatable on the stack lua_pushvalue(_state, -1); // assign the index to the copy lua_setfield(_state, -1, "__index"); // expose the table as the global "Host" lua_setglobal(_state, name); delete luaLname; } </code></pre> <p>Lets say that I want my Lua code to also be able to view keyboard state as well, and would like the Lua code to access it to look like:</p> <pre class="lang-lua prettyprint-override"><code>host = Host.new() pressed = host.Keyboard.getKeyPressed(1) </code></pre> <p>It becomes easy enough to copy the exact same pattern into the Keyboard class to setup the keyboard table, but I can't seem to come up with a good way to add that Keyboard table to my Host table. Is there a simple way to do so without messing up my pattern? Is there a better pattern that I should be using?</p> <p>I've found plenty of resources on how to create various different aspects of C-Lua APIs, but haven't really found much in terms of patterns/best practices to use while doing so. Also, I know there are libraries, such as LuaBind, that can help with this, but prefer to make my own (at least the first time).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload