Here is explained how you can bind functions from your application to Lua scripting system.
In the very beginning you would want to #include <luabind/luabind.h>. We'll use luabind for binding ;)
We'll assume you use usual Yake's ExampleApplication. class TheApp : public yake::exapp::ExampleApplication
We will also assume you have some very important function you want to use from script ( function is a member of class TheApp ):
static void myCoolFunction();
Note that this function is static. In such case we wont need to have actual TheApp object ( or ptr to object ) to call that function.
First thing to do is to load Lua scripting system. This is done by passing appropriate arguments to ExampleApplication class:
TheApp() : ExampleApplication( true /*graphics*/, false /*physics*/, true /*scripting*/,// <------------- Note this string! true /*input*/, false /*script bindings*/, false /*audio*/)
Now we have scripting system. Time to create scripting virtual machine that will do actual script processing:
yake::scripting::IVM* pVM = getScriptingSystem().createVM();
Next step is to cast a pointer to ensure we're able to access Lua functionality. We need a pointer to LuaVM!:
scripting::LuaVM* pL = dynamic_cast<scripting::LuaVM*>( pVM ); YAKE_ASSERT( pL ).error( "God, man! That virtual machine is not Lua!!!" );
You should get used to YAKE_ASSERT. It is really nice feature! Helps great in diagnostics and bug-hunting :D
Alternatively you can check for the “language” of a scripting system this way:
if (getScriptingSystem().getLanguage() != scripting::L_LUA) { /* report incorrect language or something ... */ }
Ok. One last step. Performing binding of your very important function with luabind:
using namespace luabind; module( pL->getLuaState() ) [ class_<TheApp>( "app" ) .scope [ def( "myCoolFunction", &TheApp::myCoolFunction ) ] ];
That's it! You can now make a call from Lua script ( file: example.lua ):
app.myCoolFunction();
Heh, interested how the hell to execute a script? Ok! There are 2 ways: 1st. Via script resource:
yake::scripting::IScript* pScript = getScriptingSystem().createScriptFromFile( "example.lua" ); YAKE_LOG( "Executing 'example.lua' " ); pVM->execute( pScript ); delete pScript;
2nd. Directly!:
pVM->execute( "dofile( 'example.lua' );" );
You can see the power of scripting in the 2nd approach. The string we passed to execute method is really small Lua program :). In that string we call “dofile” with path to our example script.
For those who are willing to bind functions that are members of class TheApp: you'll have to create and bind some function that returns TheApp& or TheApp* first to use myCoolFunction. You can create static method outside TheApp:
TheApp theApp; static TheApp& get() { return theApp; }
Binding will then look like:
using namespace luabind; module( pL->getLuaState() ) [ def( "getApp", &TheApp::get ), class_<TheApp>( "app" ) .def( "myCoolFunction", &TheApp::myCoolFunction ) ];
And script would call myCoolFunc like:
the_app = getApp(); the_app:myCoolFunction();
So it's your choice! :) Hope it helps :) –MJ