cocos2d-x lua与c++简单交互
cocos2d-x lua与c++简单交互
version: cocos2d-x 3.6
本文讲述lua与c++的一些简单交互:
- lua通过消息方式调用c++无参接口
- c++调用lua带参接口
1.通过消息方式调用无参接口
接收方监听消息命令,发送方发送消息请求。
- c++层监听消息
//在appdelegate启动时监听
void communication_cpp_lua::registerAllEvent()
{
Director::getInstance()->getEventDispatcher()->addCustomEventListener("LUA_TO_CPP_FACEBOOK_LOGIN", std::bind(&communication_cpp_lua::facebookLogin, this, std::placeholders::_1));
}
void communication_cpp_lua::facebookLogin(EventCustom * evt)
{
CCLOG("lua call cpp facebook login");
//FX::PluginUser *pUser = FX::PluginManager::getInstance()->getPluginUser();
//pUser->setLoginListener(this);
//pUser->login();
}
- lua层发送命令请求
local event = cc.EventCustom:new("LUA_TO_CPP_FACEBOOK_LOGIN")
cc.Director:getInstance():getEventDispatcher():dispatchEvent(event)
请确保lua层发送消息前,c++层以注册了消息监听。
2.c++回调带参数的lua接口
c++调用lua无参数的接口也可使用消息方式,但是当调用带参数接口时,如下为直接调用方式:
- lua层接口 (记得函数要放在全局里面)
-------------------------------------------------------------
-- glocal
-- cpp direct call
-------------------------------------------------------------
-- parm: int string string string string
function cc.exports.onFacebookLogin(retCode, msg, id, name, picture)
print("cc.exports.onFacebookLogin")
-- TODO
end
- c++层调用
注意事项:
- 调用时lua函数所在文件的路径
- 如果参数过多(或为数组),就将参数转为json,那么参数就只有一个了(json字符串)
void communication_cpp_lua::onLoginResult(FX::LoginResultCode ret, const char* msg)
{
CCLOG("INFO: %s ---> retCode = %d, msg = %s", __FUNCTION__, ret, msg);
FX::PluginUser *pUser = FX::PluginManager::getInstance()->getPluginUser();
FX::UserInfo userinfo = pUser->getUserInfo();
//注意函数调用,参数入栈顺序
std::vector<std::pair<std::string,__String*>> parm;
parm.push_back(std::make_pair("number", __String::createWithFormat("%d", ret)));
parm.push_back(std::make_pair("string", __String::create(msg)));
parm.push_back(std::make_pair("string", __String::create(userinfo.id)));
parm.push_back(std::make_pair("string", __String::create(userinfo.name)));
parm.push_back(std::make_pair("string", __String::create(userinfo.picture)));
this->callLuaFuncWithParam("utils/communication_lua_cpp.lua", "onFacebookLogin", &parm);
}
//带参执行Lua方法无返回值
const void communication_cpp_lua::callLuaFuncWithParam(const char* luaFileName, const char* functionName, std::vector<std::pair<std::string,__String*>>* para)
{
lua_State* ls = LuaEngine::getInstance()->getLuaStack()->getLuaState();
int isOpen = luaL_dofile(ls, FileUtils::getInstance()->fullPathForFilename(luaFileName).c_str());
if(isOpen!=0)
{
CCLOG("Open Lua Error: %i", isOpen);
}
lua_getglobal(ls, functionName);
int countnum = para->size();
for (std::vector<std::pair<std::string,__String*>>::iterator itor = para->begin(); itor != para->end(); ++itor)
{
std::string typestr = itor->first;
__String* strnr = itor->second;
if(typestr == "string")
{
lua_pushstring(ls, strnr->getCString());
}
else if(typestr == "number")
{
lua_pushnumber(ls, strnr->intValue());
}
else if(typestr == "bool")
{
lua_pushboolean(ls, strnr->boolValue());
}
else
{
CCASSERT(false, "not surport this type");
}
}
/*
lua_call
第二个参数:函数的参数个数
第三个参数:函数返回值个数
*/
lua_call(ls, countnum, 0);
//lua有返回值
// const char* iResult = lua_tostring(ls, -1);
// return iResult;
}
cocos2d-x lua与c++简单交互的更多相关文章
- Lua基本语法-lua与C#的交互(相当简单详细的例子)
lua脚本 与 C#的交互 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Lua And C# -- ...
- Lua与C的交互
Lua 与 C 的交互 Lua是一个嵌入式的语言,它不仅可以是一个独立运行的程序,也可以是一个用来嵌入其它应用的程序库. C API是一个C代码与Lua进行交互的函数集,它由以下几部分构成: 1. ...
- lua与C/C++交互概要
转 http://blog.csdn.net/wildfireli/article/details/22307635 Lua生来就是为了和C交互的,因此使用C扩展Lua或者将Lua嵌入到C当中都是非常 ...
- Lua脚本和C++交互(一)
现在,越来越多的C++服务器和客户端融入了脚本的支持,尤其在网游领域,脚本语言已经渗透到了方方面面,比如你可以在你的客户端增加一个脚本,这个脚本将会帮你在界面上显示新的数据,亦或帮你完成某些任务,亦或 ...
- lua和C++的交互(1)
/* 以前听的一个故事,当年Java的创造者讲课的时候,一开始先拿一个简单的不能简单的小例子, 不断的扩展,最后成为一个复杂而完美的程序. 一个重要之重要的概念,就是栈.Lua与别的语言交互以及交换数 ...
- lua与c的交互(函数专用)
Lua与C的交互 Lua是一个嵌入式的语言,它不仅可以是一个独立运行的程序,也可以是一个用来嵌入其它应用的程序库. C API是一个C代码与Lua进行交互的函数集,它由以下几部分构成: 1. 读写L ...
- lua Getter&Setter简单实现
lua是一门简单的语言,不带类和属性封装,但可以使用lua强大的元表模拟实现: class.lua local type = type local rawset = rawset local setm ...
- Lua与C/C++交互问题
初学lua,遇到注册C/C++交互函数问题 在lua与C/C++交互时,C/C++的注册Lua函数若是一个有返回类型(压栈)而不是获取类型的时候应该返回1而不是返回0,否则会出现在Lua中值为nil( ...
- Lua与C++的交互
这篇文章说的挺详细的:Lua与C++的交互
随机推荐
- C# 释放非托管资源
C#中资源分为托管资源和非托管资源. 托管资源由垃圾回收器控制如何释放,不需要程序员过多的考虑(当然也程序员也可以自己释放). 非托管资源需要自己编写代码来释放.那么编写好的释放非托管资源的代码(释非 ...
- mount失败
又一次遇到mount失败,提示文件系统类型错误.选项错误.有坏超级块等.之前是在ubuntu 14.04 lts desktop上挂载windows下共享文件夹遇到的.这次具体的环境如下:CentOS ...
- Java并发控制机制详解
在一般性开发中,笔者经常看到很多同学在对待java并发开发模型中只会使用一些基础的方法.比如Volatile,synchronized.像Lock和atomic这类高级并发包很多人并不经常使用.我想大 ...
- codeforces 629A Far Relative’s Birthday Cake
A. Far Relative’s Birthday Cake time limit per test 1 second memory limit per test 256 megabytes inp ...
- Java封装 properties文件操作
/** * Prop. Prop can load properties file from CLASSPATH or File object. */ public class Prop { priv ...
- Running Solr with Maven
Solr is an open source search server which is built by using the indexing and search capabilities of ...
- 一、Microsoft Dynamics CRM 4.0 SDK概述
Chapter 1. Microsoft Dynamics CRM 4.0 SDK Overview(SDK概述) You are probably reading this book because ...
- hive 子查询特别分析
Hive只支持在FROM子句中使用子查询,子查询必须有名字,并且列必须唯一:SELECT ... FROM(subquery) name ... 确认下是否一定要求列必须唯一? 建表语句 ...
- Check Box 用法
void CMyDlg::OnInitDialog() //Check1 初始化为选中状态 void CMyDlg::OnInitDialog() { CDialog::OnInitDialog(); ...
- 微软IIS服务器的最佳优化工具- IIS Tuner
dudu的 <让Windows Server 2008 + IIS 7+ ASP.NET 支持10万个同时请求>,里面涉及到需要手工调整参数的地方.在这篇文章中,我们给你介绍一个IIS ...