Game Development Reference
In-Depth Information
LuaScriptUtilities.h
#define LUA_VECTOR3_METATABLE "Vector3Type"
To support this functionality from the code, we need to let Lua know what type of user-
data it is working with when we allocate memory. The LuaScriptUtilities header
defines the metatable name of the vector type:
LuaScriptUtilities.cpp
void LuaScriptUtilities::BindVMFunctions(lua_State* const
luaVM)
{
...
luaL_newmetatable(luaVM, LUA_VECTOR3_METATABLE);
luaL_register(luaVM, NULL, LuaVector3Metatable);
...
}
When binding C++ functions to the Lua virtual machine, an additional step is added to
support vectors. The luaL_newmetatable function creates a new metatable, associat-
ing the table with the vector userdata type. Immediately after the metatable is created and
pushed onto the Lua stack, a call to luaL_register adds the metamethods listed in
LuaVector3Metatable to the metatable:
LuaScriptUtilities.cpp
int LuaScriptUtilities::PushVector3(
lua_State* const luaVM, const Ogre::Vector3& vector)
{
const size_t vectorSize = sizeof(Ogre::Vector3);
Ogre::Vector3* const scriptType =
static_cast<Ogre::Vector3*>(
lua_newuserdata(luaVM, vectorSize));
*scriptType = vector;
Search WWH ::




Custom Search