vs2012环境下,cocos2dx3.4 c++项目调用Lua的环境搭建

来源:互联网 发布:成就最高的女演员知乎 编辑:程序博客网 时间:2024/05/16 23:01
  1. 添加lua库:属性->配置属性->链接器->输入->附加依赖项:编辑添加一条数据lua51.lib
  2. 添加包含目录:属性->配置属性->c/c++->附加包含目录:编辑添加两条数据

    $(EngineRoot)external\lua$(EngineRoot)external\lua\luajit\include
  3. 请确认自己项目中是否有Lua文件夹,路径是:项目名称\cocos2d\external\lua。如果没有的话,将cocos2dx引擎目录里的Lua文件夹复制到这个目录:项目名称\cocos2d\external。cocos2dx引擎目录里的Lua文件夹目录为:cocos2d-x-3.4\external\lua。

  4. 新建一个cocos2dx项目,在需要使用lua的头文件中定义如下代码:

    extern "C"{
    #include <lua.h>
    #include <lualib.h>
    #include <lauxlib.n>
    };
  5. 在场景的初始化函数init()中输入以下代码:

    lua_State* pL = lua_open();luaL_openlibs(pL);int err = luaL_dofile(pL,"helloLua.lua");log("open:%d",err);lua_settop(pL,0);lua_getglobal(pL,"myName");int isstr = lua_isstring(pL,1);log("isstr = %d",isstr);if(isstr!=0){    const char* str = lua_tostring(pL,1);    log("getStr = %s",str);}lua_close(pL);return true;
  6. 在工程的resource目录下面新建一个lua文件:resource->添加->新建项->helloLua.lua

  7. 在新建的lua文件中输入:

    myName="beauty girl"
  8. 运行该项目,如果提示找不到lua51.lib,直接去复制过来的Lua文件夹中搜索lua51.lib和lua51.dll,将其复制到proj.win32\Debug.win32中。

  9. 运行成功后结果为
    open:0
    isstr = 1
    getStr = beauty girl

  10. 为了能让Lua文件中的print()函数能输出到VS2012的输出窗口,在运行的某个Lua文件前加上下面的代码:

function babe_tostring(...)      local num = select("#",...);      local args = {...};      local outs = {};      for i = 1, num do          if i > 1 then              outs[#outs+1] = "\t";          end          outs[#outs+1] = tostring(args[i]);      end      return table.concat(outs);  end  local babe_print = print;  local babe_output = function(...)      babe_print(...);      if decoda_output ~= nil then          local str = babe_tostring(...);          decoda_output(str);      end  end  print = babe_output;
0 0
原创粉丝点击