Cocos2d-x3.0下 Lua与C++相互调用

这里讲下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









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值