首页 > 编程语言 > 详细

lua调用有返回值的c++

时间:2016-02-25 19:42:43      阅读:426      评论:0      收藏:0      [点我收藏+]

今天项目中要在lua中调用有返回值的c++函数,对于这方面从来没用做过,所以就开始各种照猫画虎~~~~

找到了audio.lua。看到了function audio.playMusic(filename, isLoop)方法。

修改sharedEngine:playMusic( filename, isLoop)成sharedEngine:playMusic( DCConfigParams.helperFind(filename), isLoop)

打开DCConfigParams.lua,添加如下代码:

-- 读取文件
function DCConfigParams.helperFind(str)
    return DCLuaConfigParams:helperFind(str)
end

找到项目中DataEye.cpp

找到了这个地方:

 tolua_cclass(tolua_S, "DCLuaConfigParams", "DCLuaConfigParams", "", NULL);
 tolua_beginmodule(tolua_S, "DCLuaConfigParams");
    tolua_function(tolua_S, "update", tolua_DataEye_DCConfigParams_update00);
    tolua_function(tolua_S, "getParameter", tolua_DataEye_DCConfigParams_getParameter00);
    tolua_function(tolua_S, "getParameter", tolua_DataEye_DCConfigParams_getParameter01);
    tolua_function(tolua_S, "getParameter", tolua_DataEye_DCConfigParams_getParameter02);

在下面添加:

  // 解密
  tolua_function(tolua_S, "helperFind", tolua_DataEye_DCConfigParams_helperFind03);

如图:

技术分享

其实我自己是照着“tolua_function(tolua_S, "getParameter", tolua_DataEye_DCConfigParams_getParameter02)”方法去写的

接下来,坑来了,复制了tolua_DataEye_DCConfigParams_getParameter02的实现方法,修改后如下:

/* method: helperFind of class  DCLuaConfigParams */
#ifndef TOLUA_DISABLE_tolua_DataEye_DCConfigParams_helperFind03
static int tolua_DataEye_DCConfigParams_helperFind03(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isusertable(tolua_S,1,"DCLuaConfigParams",0,&tolua_err) ||
     !tolua_isnoobj(tolua_S,2,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  {
    const char *key = ((const char*)  tolua_tostring(tolua_S,2,0));
    tolua_pushstring(tolua_S, Nato::Find(key));
  }
 }
 return 0;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function ‘helperFind‘.",&tolua_err);
 return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

运行的时候,lua调用到c++的方法了,可是lua就是接受不到Nato::Find(key)返回的值。  自个本事是菜鸟,所以就自己做各种打印测试,结果就是接受不到值,可是别的方法怎么就可以呢。

后面在网上找解决办法,没有相关的 ,  然后我就去找了别人是怎么调用的。后来才发现了问题。一个很小的地方,如下图:

技术分享

原来是你往tolua_S里面放入几个值,下面的return就必须是几!!!后面改成return后,果断可以了....

 

所以。。。   没接触过的东西,当模仿时,不踩坑是不行的。。。(话说解决这个问题花了一天时间。。。还是快下班的时候在网上找到问题所在的。。。)

lua调用有返回值的c++

原文:http://www.cnblogs.com/Colored-Mr/p/5218063.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!