-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Integrate Lua with C++
By :

In the previous section, we returned the pointer to the class instance to Lua as a userdata. In this section, we will export member functions for the userdata. To do this, we need to set the metatable for the userdata.
In Chapter 5, we learned that each table can have a metatable. Similarly, each userdata can also have a metatable. In Chapter 2, we learned that in Lua code, the metatable needs to be set during object creation.
Here, we need to set the metatable during object creation in C++ code. Instead of creating a new metatable for each object, we can create a single metatable and store it in the registry. Then, each object only needs to reference this single metatable. This will increase efficiency and reduce memory footprint. The Lua library even provides helper functions for this.
First, let’s see the code; an explanation will follow. Replace the content for luaNew
, as follows:
const std::string...