这里讲下Lua与C++之间如何实现相互调用
原文连接:http://blog.csdn.net/qqmcy/article/details/26052771
DJLCData.h 实现类
//
// DJLCData.h
// uitestLua
//
// Created by 杜甲 on 14-5-17.
//
//
#ifndef __uitestLua__DJLCData__
#define __uitestLua__DJLCData__
#include "cocos2d.h"
#include "CCLuaEngine.h"
USING_NS_CC;
using namespace std;
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
class DJLCData {
public:
static DJLCData* getInstance();
/*
getLuaVarString:调用Lua全局string
luaFilename = lua文件名
varName = 所要取Lua中的变量名
*/
const char* getLuaVarString(const char* luaFileName,const char* varName);
/*
* @brief getLuaOneOfTable:调用Lua全局table中的一个元素
luaFileName = lua文件名
varName = 所要取Lua中的table变量名
keyName = 所要取Lua中的table中某一个元素的Key
*/
const char* getLuaVarOneOfTable(const char* luaFileName,const char* varName,const char* keyName);
/*
* getLuaVarTable:调用lua全局table
luaFileName = lua文件名
varName = 所要取的table变量名
(注:返回的是所有的数据)
*/
const char* getLuaVarTable(const char* luaFileName,const char* varName);
/*
callLuaFunction:调用lua函数
luaFileName = lua文件名
functionName = 所要调用Lua中的函数名
*/
const char* callLuaFunction(const char* luaFileName,const char* functionName);
/***************** lua -> C++ **************/
void callCppFunction(const char* luaFileName);
private:
static int cppFunction(lua_State* ls);
static bool _isFirst;
static DJLCData* _shared;
const char* getFileFullPath(const char* fileName);
};
#endif /* defined(__uitestLua__DJLCData__) */
DJLCData.cpp
//
// DJLCData.cpp
// uitestLua
//
// Created by 杜甲 on 14-5-17.
//
//
#include "DJLCData.h"
bool DJLCData::_isFirst;
DJLCData* DJLCData::_shared;
DJLCData* DJLCData::getInstance()
{
if (_shared == nullptr) {
_shared = new DJLCData();
}
return _shared;
}
const char* DJLCData::getLuaVarString(const char *luaFileName, const char *varName)
{
lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState();
int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName));
if (isOpen != 0) {
log("Open Lua Error:%i",isOpen);
return nullptr;
}
lua_settop(ls, 0);
lua_getglobal(ls, varName);
int statesCode = lua_isstring(ls, 1);
if (statesCode != 1) {
log("Open Lua Error : %i",statesCode);
return nullptr;
}
const char* str = lua_tostring(ls, 1);
lua_pop(ls, 1);
return str;
}
const char* DJLCData::getLuaVarOneOfTable(const char *luaFileName, const char *varName, const char *keyName)
{
lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState();
int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName));
if (isOpen != 0) {
log("Open Lua Error : %i",isOpen);
return nullptr;
}
lua_getglobal(ls, varName);
int statesCode lua_istable(ls, -1);
if (statesCode != 1) {
log("Open Lua Error: %i",statesCode);
return nullptr;
}
lua_pushstring(ls, keyName);
lua_gettable(ls, -2);
const char* valueString = lua_tostring(ls, -1);
lua_pop(ls, -1);
return valueString;
}
const char* DJLCData::getLuaVarTable(const char *luaFileName, const char *varName)
{
lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState();
int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName));
if (isOpen != 0) {
log("Open Lua Error:%i",isOpen);
return nullptr;
}
lua_getglobal(ls, varName);
int it = lua_gettop(ls);
lua_pushnil(ls);
string result = "";
while (lua_next(ls, it)) {
string key = lua_tostring(ls, -2);
string value = lua_tostring(ls, -1);
result = result + key + ":" + value + "\t";
lua_pop(ls, 1);
}
lua_pop(ls, 1);
return result.c_str();
}
const char* DJLCData::callLuaFunction(const char *luaFileName, const char *functionName)
{
lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState();
int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName));
if (isOpen) {
log("Open Lua Error:%i",isOpen);
return nullptr;
}
lua_getglobal(ls, functionName);
lua_pushstring(ls, "shen");
lua_pushnumber(ls, 23);
lua_pushboolean(ls, true);
/*
lua_call
第一个参数:函数的参数个数
第二个参数:函数返回值个数
*/
lua_call(ls, 3, 1);
const char* iResult = lua_tostring(ls, -1);
return iResult;
}
void DJLCData::callCppFunction(const char *luaFileName)
{
lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState();
/*
Lua调用的C++的函数必须是静态的
*/
lua_register(ls, "cppFunction", cppFunction);
int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName));
if (isOpen != 0) {
log("Open Lua Error:%i",isOpen);
return;
}
}
int DJLCData::cppFunction(lua_State *ls)
{
int luaNum = (int)lua_tonumber(ls, 1);
char* luaStr = (char*)lua_tostring(ls, 2);
log("Lua调用cpp函数时传来的两个参数:%i,%s",luaNum,luaStr);
/*
返给Lua的值
*/
lua_pushnumber(ls, 321);
lua_pushstring(ls, "shenqi");
/*
返给Lua值个数
*/
return 2;
}
const char* DJLCData::getFileFullPath(const char *fileName)
{
//src// 这里的//是指我的lua文件放在src这个目录下
std::string str = StringUtils::format("src//%s",fileName);
return FileUtils::getInstance()->fullPathForFilename(str).c_str();
}
hello2.lua
function myadd(x, y)
return x + y
end
luaStr = "shenqi"
luaTable = {name = "xiaonan",age = "20"}
function luaLogString(_logStr,_logNum,_logBool)
print("Lua 脚本打印从C传来的字符串:",_logStr,_logNum,_logBool)
return "Call Lua function OK"
end
function call_cpp(_logStr,_logNum,_logBool)
num,str = cppFunction(999,"lua string")
print("从cpp函数中获得两个返回值:",num,str)
end
调用方法:
log("%s",DJLCData::getInstance()->getLuaVarString("hello2.lua", "luaStr"));
log("%s",DJLCData::getInstance()->getLuaVarOneOfTable("hello2.lua", "luaTable", "name"));
log("Table = %s",DJLCData::getInstance()->getLuaVarTable("hello2.lua", "luaTable"));
log("Call Lua Function Back :%s",DJLCData::getInstance()->callLuaFunction("hello2.lua", "luaLogString"));
DJLCData::getInstance()->callCppFunction("hello2.lua");
DJLCData::getInstance()->callLuaFunction("hello2.lua", "call_cpp");
cocos2d: shenqi
cocos2d: xiaonan
cocos2d: Table = name:xiaonan age:20
cocos2d: [LUA-print] Lua 脚本打印从C传来的字符串: shen 23 true
cocos2d: Call Lua Function Back :Call Lua function OK
cocos2d: Lua调用cpp函数时传来的两个参数:999,lua string
cocos2d: [LUA-print] 从cpp函数中获得两个返回值: 321 shenqi