-------------------------- .h ---------------------------
#ifndef __HELLO_LUA_H_
#define __HELLO_LUA_H_
#include "cocos2d.h"
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
};
using namespace cocos2d;
class HelloLua : public Layer {
public:
CREATE_FUNC(HelloLua);
virtual bool init();
static Scene* scene();
private:
void demo1();
void demo2();
void demo3();
void demo4();
void demo5();
public:
static int getNumber(int num);
static int int_CPP_method_name(lua_State* pL);
static int dugaoda_calculate_average(lua_State* pL);
};
#endif
-------------------------- .cpp ---------------------------
#include "HelloLua.h"
Scene* HelloLua::scene() {
Scene* scene = Scene::create();
Layer* layer = HelloLua::create();
scene->addChild(layer);
return scene;
}
bool HelloLua::init()
{
demo4();
return true;
}
///* 示例1:简单获取lua的全局变量 */
void HelloLua::demo1()
{
// 2015年2月4日 15:37:04
// API有一系列压栈的函数,它将每种可以用C来描述的Lua类型压栈
//Lua和C++的通信流程代码实例
lua_State* pL = lua_open();//创建一个lua环境
//luaopen_base(pL);luaopen_math(pL);luaopen_string(pL);
//Lua有一些标准库,要使用这些库,就要用luaopen_**去加载这些库
luaopen_base(pL);
luaopen_math(pL);
luaopen_string(pL);
/* 执行Lua脚本,返回0代表成功 */
int err = luaL_dofile(pL, "res/helloLua.lua");
CCLOG("open : %d", err);
/* 重置栈顶索引 */
lua_settop(pL, 0);
lua_getglobal(pL, "myName");
/* 判断栈顶的值的类型是否为String, 返回非0值代表成功 */
int isstr = lua_isstring(pL, 1);
CCLOG("isstr = %d", isstr);
/* 获取栈顶的值 */
const char* str = lua_tostring(pL, 1);
CCLOG("getStr = %s", str);//beauty girl dugaoda
lua_getglobal(pL, "hello");
/* 重置栈顶索引 */
int isstr1 = lua_isstring(pL, 2);
CCLOG("isstr1 = %d", isstr1);
const char* value2 = lua_tostring(pL, 2);
CCLOG("value2 = %s", value2);//dugaoda
/* 重置栈顶索引 */
lua_settop(pL, 0);
lua_getglobal(pL, "li");
int value3 = lua_tonumber(pL, 1);
CCLOG("value3 = %d", value3);//3
//lua_getglobal(pL, "myName");
//int number2 = lua_tonumber(pL, 2);
// CCLOG("number2 = %d", number2);// 输出1
/* 清除字符串 */
//lua_pop(pL, 1);
/* 再去判断栈顶的值的类型是否为String */
int isstr5= lua_isstring(pL, 1);// 这个时候已经清除字符串了 所以isstr1为nil
CCLOG("isstr5 = %d", isstr5);
//释放内存
lua_close(pL);
}
/* 示例2:获取lua table的数据 */
void HelloLua::demo2()
{
/* 初始化 */
lua_State* pL = lua_open();
luaopen_base(pL);
/* 执行脚本 */
luaL_dofile(pL, "res/helloLua.lua");
/* 取得table变量,在栈顶 */
lua_getglobal(pL, "hello_dugaoda_Table");
/* 将C++的字符串放到Lua的栈中,此时,栈顶变为“name”, helloTable对象变为栈底 */
lua_pushstring(pL, "name");//到Lua的栈中,去table寻找的过程
/*
从table对象寻找“name”对应的值(table对象现在在索引为-2的栈中,也就是当前的栈底),
取得对应值之后,将值放回栈顶
*/
lua_gettable(pL, -2);//放到栈上
//判断栈顶元素类型
int istablestr1 = lua_isstring(pL, -1);
CCLOG("istablestr1 = %d", istablestr1);
/* 现在表的name对应的值已经在栈顶了,直接取出即可 */
const char* sName = lua_tostring(pL, -1);
CCLOG("name = %s", sName);
lua_pushstring(pL, "year");//到Lua的栈中,去table寻找的过程
lua_gettable(pL, -3);//放到栈上 注意 如果使用lua_gettable(pL, -1);会发生错误
int istableint1 = lua_isnumber(pL, -1);
CCLOG("istableint1 = %d", istableint1);
/* 现在表的name对应的值已经在栈顶了,直接取出即可 */
int top = lua_tonumber(pL, -1);
CCLOG("top = %d", top);
}
/* C++调用lua的函数 */
void HelloLua::demo3() {
lua_State* pL = lua_open();
luaopen_base(pL);
/* 执行脚本 */
luaL_dofile(pL, "res/helloLua.lua");
/* 把helloAdd函数对象放到栈中 */
lua_getglobal(pL, "helloAdd");
/* 把函数所需要的参数入栈 */
lua_pushnumber(pL, 10);
lua_pushnumber(pL, 5);
/*
执行函数,第一个参数表示函数的参数个数,第二个参数表示函数返回值个数 ,
Lua会先去堆栈取出参数,然后再取出函数对象,开始执行函数
*/
lua_call(pL, 2, 1);
int iResult = lua_tonumber(pL, -1);
CCLOG("iResult = %d", iResult);
lua_getglobal(pL, "helltess");
lua_pushnumber(pL, 39);
lua_pushnumber(pL, 23);
lua_call(pL, 2, 1);
int myResult1 = lua_tonumber(pL, -1);
CCLOG("myResult1 = %d", myResult1);
}
/* Lua调用C++的函数 */
void HelloLua::demo4() {
lua_State* pL = lua_open();
luaopen_base(pL);
/* C++的函数和封装函数都必须是静态的,不知道可不可以不是静态的?当然不可以! */
//lua_register(pL, "lua_abc", cpp_GetNumber); //在lua中使用cpp_GetNumber表示调用C++中cpp_GetNumber()
lua_register(pL, "int_lua_method_name", int_CPP_method_name);
lua_register(pL, "dugaoda_calculate_average_inlua", dugaoda_calculate_average);
//2个方法互相独立不会影响
luaL_dofile(pL, "res/helloLua.lua");
lua_close(pL);
}
void HelloLua::demo5() {
}
int HelloLua::getNumber( int num ) {
CCLOG("getNumber num = %d", num);
return num + 1;
}
//lua调用c++函数1
int HelloLua::int_CPP_method_name( lua_State* pL )
{
//* 从栈顶中取一个值,这个值就是lua中lua_abc的参数,这里是123321 */
int num = (int)lua_tonumber(pL, -1);
//* 调用getNumber函数,将返回值入栈 */
lua_pushnumber(pL, getNumber(num));//lua中会从栈上面获取getNumber(num)的返回值, 这里是123321+1
//* 返回值个数,getNumber只有一个返回值,所以返回1 */
return 1;
}
//lua调用c++函数2
int HelloLua::dugaoda_calculate_average( lua_State* pL )
{
int get_canshu = lua_gettop(pL);///* 得到参数个数 */ lua中运行函数的时候会把参数放到栈上面
int int_sum = 0;
for(int i = 1; i < get_canshu; i++)
{
int_sum = int_sum + lua_tonumber(pL, i); //从栈底开始获取元素
}
int int_average = int_sum / get_canshu;
lua_pushnumber(pL, int_average);//将平均数放到栈上面
lua_pushnumber(pL, int_sum);
return 2;//返回参数的个数
}
----------------------- HelloLua.lua------------------------------
myName = "beauty girl dugaoda"
hello = "dugaoda"
li = 3
hello_dugaoda_Table = {name = "dugaoda", year = 10}
dugaodaTable = {year = 10, he= "10"}
function helloAdd(num1, num2)
return (num1 + num2)
end;
function helltess(payer1, player2)
return payer1
end
local average, dugaodameathersum = dugaoda_calculate_average_inlua(1, 2, 3, 34, 67, 34)
local dugaodameather = int_lua_method_name(123321)