How to use userdata
From this section onwards, we will transform the Destinations
class from the previous chapter and register it to Lua as a type so that Lua code can create objects from it. Before we dive into the details, let’s make some high-level changes to show what we want at the project level.
Preparing the C++ type
In the previous chapter, we passed a name to the constructor for the Destinations
class because we created its instances in C++ and needed to set a name to Lua for each instance.
In this chapter, we will export the C++ type to Lua. Lua will create the object and assign it a name. All changes will be in the Destinations
class. The mechanism we implemented in the Lua executor to register C++ modules is flexible enough to support the registration of C++ types as well.
To reflect this change and difference, we will change the constructor and how the module name is provided. In Destinations.h
, change the constructor, as follows:
class Destinations...