在脚本中使用类的成员函数
#include "luaplus.h"
using namespace LuaPlus;
![](/Images/OutliningIndicators/None.gif)
template< class T >
class LuaExport
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
LuaExport(const char* pszName, LuaPlus::LuaState* pState = 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//TDAssert(s_pMe == 0);
s_pMe = static_cast< T* >(this);
![](/Images/OutliningIndicators/InBlock.gif)
LuaPlus::LuaState* theState = pState; //? pState : CScriptSystem::_GetLuaState();
![](/Images/OutliningIndicators/InBlock.gif)
s_pMetaTable = new LuaPlus::LuaObject;
*s_pMetaTable = theState->GetGlobals().CreateTable(pszName);
s_pMetaTable->SetObject("__index", *s_pMetaTable);
![](/Images/OutliningIndicators/InBlock.gif)
LuaObject obj = theState->BoxPointer(s_pMe);
obj.SetMetaTable(*s_pMetaTable);
theState->GetGlobals().SetObject(pszName, obj);
}
![](/Images/OutliningIndicators/InBlock.gif)
virtual ~LuaExport()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
delete s_pMetaTable; s_pMetaTable = 0;
s_pMe = 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
template <class Callee>
void RegisterFunction(const char* pszFuncName, int (Callee::*func)(LuaPlus::LuaState*))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
s_pMetaTable->RegisterObjectFunctor(pszFuncName, func);
}
![](/Images/OutliningIndicators/InBlock.gif)
//取得自己
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
static T* GetMe(void)
{ return s_pMe; }
![](/Images/OutliningIndicators/InBlock.gif)
protected:
static T* s_pMe; //自身实例,全局变量
static LuaPlus::LuaObject* s_pMetaTable; //MetaTable
![](/Images/OutliningIndicators/InBlock.gif)
private:
// unimplemented constructors / assignment
LuaExport(const LuaExport& obj);
LuaExport& operator=(const LuaExport& obj);
};
![](/Images/OutliningIndicators/None.gif)
extern LuaPlus::LuaState* GetLuaState();// 测试main.cpp
class player : public LuaExport< player >
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
player():
LuaExport< player >( "pp", GetLuaState() )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
int LS_PrintNumber(LuaState* state)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LuaStack args(state);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (args[2].IsNumber())
{
printf("%f\n", args[2].GetNumber());
}
else if ( args[2].IsString() )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
printf("%s\n", args[2].GetString() );
}
return 0;
}
int UI_SetTipText( lua_State *state )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
LuaStack args(state);
if ( args[2].IsString() )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
const char *szData = args[2].GetString();//lua_tostring(state, 1);
if( szData )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
printf("%s\n", szData );
}
}
![](/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](/Images/OutliningIndicators/InBlock.gif)
};
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
player* LuaExport< player >::s_pMe = 0;
LuaPlus::LuaObject* LuaExport< player >::s_pMetaTable = 0;
![](/Images/OutliningIndicators/None.gif)
int _tmain(int argc, _TCHAR* argv[])
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
player* pPlayer = new player();
pPlayer->RegisterFunction( "PrintNumber", &player::LS_PrintNumber );
pPlayer->RegisterFunction( "SetTips", &player::LS_PrintNumber );
// Register your functions.
![](/Images/OutliningIndicators/InBlock.gif)
int nRet = GetLuaState()->DoFile( "main.lua" );
if ( nRet )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
printf("错误信息: %s", GetLuaState()->StackTop().GetString());
}
getchar();
return 0;
}// main.lua
pp.PrintNumber(1000)
![](/Images/OutliningIndicators/None.gif)
1.需要注意的是,类的成员函数中的参数 默认第一个为this
所以 要活得参数的时候 下标默认加1
if (args[2].IsNumber())
{
printf("%f\n", args[2].GetNumber());
}
posted on 2011-07-18 13:27
风轻云淡 阅读(1233)
评论(0) 编辑 收藏 引用 所属分类:
LuaPlus