-
Book Overview & Buying
-
Table Of Contents
Integrate Lua with C++
By :
The Lua hello function we used in the previous section is a good example to demonstrate global variables, but it is not how you would usually implement such a feature. Now consider a more suitable implementation:
function greetings(whom) return "Hello " .. whom end
This Lua greetings function expects whom as a function parameter and returns the greeting string instead of printing it out. You can use the greeting string in more flexible ways, for example, by using it on a GUI window.
Earlier in this chapter, while learning how to execute Lua scripts, we implemented the execute function in our executor. We can invoke greetings with it:
LuaExecutor lua;
lua.executeFile("script.lua");
lua.execute("greetings('Lua')");
But this is not C++ calling a Lua function; it is a Lua script calling a Lua function. C++ just compiles the Lua script and has no access to the function’s return value...