您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx——lua

2016-06-15 23:35 483 查看
lua是一个非常厉害的东西!

lua最强大的优势在于 不用重新编译项目就可以修改游戏逻辑 大大缩减了开发进程

首先 要想与cocos2dx游戏开发中使用lua 必须解决的问题就是c++和lua的通信问题 c++和lua的通信是通过
堆栈  和 全局表 实现的

我们先来看看整体通信过程:

假若我们lua文件中有此语句: helloLua = "myFirstLua"

我们想要获取这个字符串



1.c++将helloLua放入堆栈之中(很明显放在栈顶)

2.lua于栈顶获取到helloLua 此时helloLua被取出了 此时堆栈为空了

3.lua于全局表中查找helloLua对应的字符串“myFirstLua”

4.全局表将“myFirstLua”返回给lua

5.lua将“myFirstLua”放回栈顶

6.c++于栈顶获取到“myFirstLua”

c++和lua通信的几本原理就是这样的

下面我们来看看相关的具体代码:

1.首先我们要引入lua库 并且 包含相关目录

2.开始使用

extern "C"//这些都是c的库函数 所以要extern

{

#include<lua\lua\lua.h>

#include<lua\lua\lualib.h>

#include<lua\lua\lauxlib.h>

}

#include"cocos2d.h"

USING_NS_CC;

class HelloLua :public Layer

{

public:

    virtual bool init();

    static Scene* createScene();

    CREATE_FUNC(HelloLua);

};
//获取lua文件中的字符串

bool HelloLua::init()

{

    

    //一切lua脚本都运行于一个动态分配的lua_state数据结构之中

    lua_State* pL = lua_open();

    //引入相关要使用的标准库

    luaopen_string(pL);

    luaopen_base(pL);

    luaopen_math(pL);

    //执行lua脚本

    int err = luaL_dofile(pL,"helloLua.lua");

    log("err=%d", err);//返回0 代表执行成功

    //获取字符串

    //1.重置栈顶索引

    lua_settop(pL, 0);

    //lua于栈顶获取字符串名 并且获取字符串后 放回栈顶

    lua_getglobal(pL, "myName");

    

    //判断是否是字符串

    int isStr = lua_isstring(pL, 1);

    log("isStr:%d", isStr);//返回非0代表成功

    

    //获取栈顶的值

    if (isStr != 0)

    {

        const char* str = lua_tostring(pL, 1);

        log("str = %s", str);

    }

    //关闭lua

    lua_close(pL);

    return true;

}

ps:lua文件就置于resource文件夹中 只需新建一个txt并且将后缀改为.lua

/获取lua文件中的table中的字符串

bool HelloLua::init()

{

    

    //一切lua脚本都运行于一个动态分配的lua_state数据结构之中

    lua_State* pL = lua_open();

    //引入相关要使用的标准库

    luaopen_string(pL);

    luaopen_base(pL);

    luaopen_math(pL);

    //执行lua脚本

    int err = luaL_dofile(pL,"helloLua.lua");

    log("err=%d", err);//返回0 代表执行成功

    //获取字符串

    //1.重置栈顶索引

    lua_settop(pL, 0);

    //将table置于栈顶

    lua_getglobal(pL, "helloTable");

    //将字符串名放于栈顶

    lua_pushstring(pL, "name");

    //获取table中的name字符串

    lua_gettable(pL,-2);//此时栈内有 name 和 table 用负数索引所以为-2 将name取出后将结果字符串放入 此时-1处就是结果字符串

    //读取字符串

    const char* str = lua_tostring(pL, -1);

    log("str=%s", str);

    

    //关闭lua

    lua_close(pL);

    return true;

}

//c++调用lua函数

bool HelloLua::init()

{

    

    //一切lua脚本都运行于一个动态分配的lua_state数据结构之中

    lua_State* pL = lua_open();

    //引入相关要使用的标准库

    luaopen_string(pL);

    luaopen_base(pL);

    luaopen_math(pL);

    //执行lua脚本

    int err = luaL_dofile(pL,"helloLua.lua");

    log("err=%d", err);//返回0 代表执行成功

    //获取字符串

    //1.重置栈顶索引

    lua_settop(pL, 0);

    //将table置于栈顶

    lua_getglobal(pL, "helloAdd");

   //将参数传入

    lua_pushnumber(pL, 10);

    lua_pushnumber(pL, 5);

    //调用函数  第一个数字是参数个数 第二个数字是返回值个数 返回值会放在栈顶

    lua_call(pL, 2, 1);

    

    //读取字符串

    const int str = lua_tonumber(pL, 1);

    log("str=%d", str);

    

    //关闭lua

    lua_close(pL);

    return true;

}

下面是我的lua文件内容:

myName = "beauty girl"

helloTable = {name = "adh",ID = 12345}

function helloAdd(num1,num2)

 return (num1+num2);

 end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: