Game Development Reference
In-Depth Information
LuaState* pState; // assume this is a valid LuaState pointer
LuaObject globals = pState->GetGlobals();
globals.RegisterDirect(
Square
, &Square);
That
'
s all there is to it. This binds the global C++ function Square() to the name
Square
in the globals table in Lua. That means anywhere in your Lua code, you can
do this:
x = Sqaure(5) -- x will be 25
This works for static functions as well. This is how you would bind a static function
to a global Lua function:
globals.RegisterDirect(“SomeFunction”, &SomeClass::SomeStaticFunction);
Notice how the arguments are deciphered and sent through to the function automat-
ically. This is one of the really nice things about using systems like LuaPlus; it takes
care of a lot of the overhead of marshalling data across the C++/Lua boundary.
You can use an overloaded version of RegisterDirect() to bind member func-
tions of C++ classes. If you have an object you know isn
t going to be destroyed
while the script has access to it, you can bind the pointer and function pair directly
by providing a reference to the object as the second parameter.
'
class SingletonClass
{
public:
void MemberFunction(int param);
virtual VirtualMemeberFunction(char* str);
};
SingletonClass singletonInst;
LuaState* pLuaState; // once again, assume this is valid
// Register the member function
pLuaState->GetGlobals().RegisterDirect(
, singletonInst,
&SingletonClass::MemberFunction);
MemberFunction
// You can register virtual functions too, it doesn
'
t matter. The correct
// version will get called.
pLuaState->GetGlobals().RegisterDirect(
, singletonInst,
&SingletonClass::VirtualMemberFunction);
VirtualMemberFunction
In the example above, two member functions along with their instances are bound to
global Lua functions. This still only gets us part of the way there since the reference
you bind with RegisterDirect() never changes. What we really need is a way to
Search WWH ::




Custom Search