Namespaces

Everything inside Lua Bridge must be declared within a namespace. These namespaces have no connection to C++ namespaces, and within Lua, they are just tables. The concept of a namespace is similar to the concept of a Lua module being loaded as a table.

A global namespace exists, and you can register functions, variables, and classes to that namespace, but it's considered bad practice. You can get the global namespace with the following call:

getGlobalNamespace(lua_State*)

Once you have the global namespace, you can create your own namespace inside it with the beginNamespace(const char*) call. This function takes a string with the name of the namespace as an argument. Once you are done working with the namespace, you must close it with the endNameSpace() call. If you want to add more classes in different parts of the code in a namespace, it can be opened multiple times. The following code sample demonstrates this:

getGlobalNamespace(L)
.beginNamespace("foo")
// Add things to foo
.endNamespace()

// More code
getGlobalNamespace(L)
.beginNamespace("foo")
// Add more things to foo
.endNamespace()

If you were to make a variable named bar inside the preceding foo namespace, accessing the bar variable would require using the namespace as follows:

print("bar: " .. foo.bar)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.255.87