XScript介绍
lua是一种轻量小巧的脚本语言,免费开源,简单易学,c/c++这类低级语言胜在能够直接与操作系统打交道,从而能够最大限度的利用系统资源,但是写逻辑不太方便,“C++/lua”是游戏界比较常用的一种开发语言解决方案,用c++做服务器底层,再嵌入lua编写业务逻辑是比较常用的一种解决方案,用c++做服务器底层,再嵌入lua编写业务逻辑,这种组合能够比较好的平衡性能与开发效率。
XScript是一个跨平台c++/lua交互库,内置lua面向框架,支持远程调试,能够编译成静态库或者动态库,能够快速集成到新项目中。
git地址 https://gitee.com/SH-Jun_admin/XScript.git
注册cpp全局函数
#include <stdio.h>
#include <stdlib.h>
#include <core/XScript.h>
#include <luabinder/CScriptLua.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <direct.h>
#endif
using namespace XS;
void GlobalFun(const char* szContent)
{
printf("GlobalFun %s\n", szContent);
}
REGIST_GLOBALFUNCTION(GlobalFun)
const char* szLuaPath = "E:/open-src/XScript/samplers/BlogTest/lua/test.lua";
int main(int argc, char** args)
{
CScriptBase* pScript = new CScriptLua(nullptr, 123456);
char szWorkDir[2048];
getcwd(szWorkDir, ELEM_COUNT(szWorkDir));
pScript->AddSearchPath(szWorkDir);
pScript->RunFile(szLuaPath);
char ch = getchar();
delete pScript;
return 0;
}
print("this is a test file");
GlobalFun("this is a GlobalFun")
注册并继承c++ class
#include <stdio.h>
#include <stdlib.h>
#include <core/XScript.h>
#include <luabinder/CScriptLua.h>
#include <string>
#ifndef _WIN32
#include <unistd.h>
#else
#include <direct.h>
#endif
using namespace std;
using namespace XS;
class CActor
{
public:
CActor(const char* szName, const char *szIcon)
{
m_szName = szName;
m_szHeadIcon = szIcon;
}
~CActor(){};
string GetName(){
return m_szName;
}
virtual int GetClassID(){ return 0; }
private:
string m_szName;
string m_szHeadIcon;
};
void GlobalFun(CActor* pActor)
{
printf("void GlobalFun(CActor* pActor)\n");
printf("GlobalFun %d %s\n", pActor->GetClassID(), pActor->GetName().c_str());
}
REGIST_GLOBALFUNCTION(GlobalFun)
DEFINE_CLASS_BEGIN(CActor)
REGIST_CONSTRUCTOR(const char*, const char*)
REGIST_DESTRUCTOR()
REGIST_CLASSFUNCTION(GetName)
REGIST_CALLBACKFUNCTION(GetClassID)
DEFINE_CLASS_END();
const char* szLuaPath = "E:/open-src/XScript/samplers/BlogTest/lua/test.lua";
int main(int argc, char** args)
{
CScriptBase* pScript = new CScriptLua(nullptr, 123456);
char szWorkDir[2048];
getcwd(szWorkDir, ELEM_COUNT(szWorkDir));
pScript->AddSearchPath(szWorkDir);
pScript->RunFile(szLuaPath);
char ch = getchar();
delete pScript;
return 0;
}
print("this is a test file");
LuaActor = LuaActor or class(CActor)
function LuaActor:GetClassID()
print("this is LuaActor:GetClassID")
return 35
end
local actor = LuaActor:new("bob", "xxx.png")
GlobalFun(actor)