(Lua) C++ 呼叫 Lua 的變數、函式
簡單的在C++裡頭與Lua交互操作
首先提供 Lua 的簡單範例
print(" Lua 2019/01/07 !!!")
-- Variable
monster_type = "Ghost"
blood = 99.9
-- Table
x_table = {, , }
-- Function
function f(var)
return var * var + * var +
end
呼叫變數的辦法
int main(int argc, const char *argv[])
{
string scriptnema = "main.lua";
int luaError;
lua_State *L = luaL_newstate(); if (L == NULL)
{
cout << "luaL_newstate faile !!!" << endl;
return -;
} luaL_openlibs(L);
// first getbloable parameter that it will be first on the stack.
// stack index number will follow small to large [1 ... 5] or [-5 ... -1]
lua_getglobal(L, "blood");
lua_getglobal(L, "monster_type"); double blood = lua_tonumber(L, ); // index = 1 or -2
string type = lua_tostring(L, ); // index = -1 or 2 cout << "blood = " << blood << endl;
cout << "monster_typa = " << type.c_str() << endl; }
呼教 table的方式
int main(int argc, const char *argv[])
{
//cout << "lua test platform!!!" << endl;
string scriptnema = "main.lua";
int luaError;
lua_State *L = luaL_newstate(); if (L == NULL)
{
cout << "luaL_newstate faile !!!" << endl;
return -;
} luaL_openlibs(L);
luaL_dofile(L, scriptnema.c_str()); lua_getglobal(L, "x_table");
lua_Integer array_len = luaL_len(L, -);
cout << "array_len = " << array_len << endl; for (int i = ; i > ; i--){
lua_rawgeti(L, -, i); // lua_rawgeti(lua stack, index, value)
cout << lua_tonumber(L, -) << endl;
lua_pop(L, );
}
}
在 C++ 輸入變數給 Lua 且 return 結果
int luaf(lua_State *L){
double sum;
lua_getglobal(L, "f");
lua_pushnumber(L, );
lua_call(L, , ); // lua_call(lua stack, input num, output num)
sum = (int)lua_tonumber(L, );
lua_pop(L, );
return sum;
}
int main(int argc, const char *argv[])
{
//cout << "lua test platform!!!" << endl;
string scriptnema = "main.lua";
int luaError;
lua_State *L = luaL_newstate();
if (L == NULL)
{
cout << "luaL_newstate faile !!!" << endl;
return -;
}
luaL_openlibs(L);
luaL_dofile(L, scriptnema.c_str());
cout << luaf(L) << endl;
lua_close(L);
system("pause");
}
以上是 Lua --> Stack 基本API應用,希望最後是把所有的API都玩過一遍。
這樣之後學習與應用時可以更加靈活
(Lua) C++ 呼叫 Lua 的變數、函式的更多相关文章
- 学JS的心路历程 -函式(三)this
this是什么,取决于被呼叫的呼叫地点. 昨天有提到说,呼叫函式时候会传递隐含参数:arguments和this并讲解了arguments,今天我们就来探讨this吧! 什么是this 我们都会呼叫函 ...
- 学习JS的心路历程-函式(一)
前几天有间单提到该如何声明函式及在Hositing中会发生什么事,但是函式的奥妙不仅于此. 身为一个使用JS的工程师,我们一定要熟悉函式到比恋人还熟! 这几天将会把函式逐一扒开跟各位一起探讨其中的奥妙 ...
- (Lua) C++ 加入 Lua 環境擴充應用強度
Lua 在網上有非常多的介紹,就是一個小而巧的語言,可以放入嵌入式系統 也可以在一般的應用上非常強大,這邊主要記錄如何讓Lua加入C++裡頭應用 Lua source code 是以 C 語言下去編寫 ...
- t100 常用公用變數
g_enterprise 目前的企業代碼,將限制使用者所能閱讀的資料內容g_prog 目前執行的作業編號,用於變換畫面顯示資料與產生系統資訊,不可變更g_code 目前執行的程式代碼(4gl)名稱,不 ...
- Lua 架构 The Lua Architecture
转载自:http://magicpanda.net/2010/10/lua%E6%9E%B6%E6%9E%84%E6%96%87%E6%A1%A3/ Lua架构文档(翻译) 十 102010 前段时间 ...
- vJine 第三波 之 Lua 来袭 vJine.Lua
vJine.Lua vJine.Lua是Lua语言的C#封装库,可实现通过C#直接运行Lua脚本并与Lua脚本交互的功能. 1. 授权: MPL2.0 相关资源: nuget:(https://www ...
- VC和VS调用Lua设置以及Lua C API使用。
通过c++调用lua 脚本, 环境VC++6.0 lua sdk 5.1.4 在调用前先认识几个函数.1.调用lua_open()将创建一个指向Lua解释器的指针.2. luaL_ope ...
- keil c51 本變數型態(Variable Type)
本變數型態(Variable Type): 類 別 符號位元 位元組(bytes) 表 示 法 數 值 範 圍 整 數 有 2 int(short) -32768~0~>32767 4 long ...
- lua调用不同lua文件中的函数
a.lua和b.lua在同一个目录下 a.lua调用b.lua中的test方法,注意b中test的写法 _M 和 a中调用方法: b.lua local _M = {}function _M.test ...
随机推荐
- 1-求组合数(c(n, m))的几种方法
1.求C(n, m) 动态规划(递归+记忆数组) 递推关系为:C(n, m) = C(n-1, m) + C(n - 1, m - 1),C(n, m)表示为从n个数中选出m个出来,可以基于最后一个元 ...
- Redis 安装配制
Redis 安装配制 redis 安装分为单机安装.伪集群安装.集群安装. Redis 下载地址:http://www.redis.cn/download.html Redis 在线测试工具:http ...
- log4j 使用记录
又很懒了.. 好久没来了,必须自我批评一下... ===========================我是分割线============================= 鼎鼎大名的log4j,要说 ...
- typedef char int8; 这样定义的好处?
用typedef定义int8代表char:然后用int8去定义其他变量.一旦系统中char不再是占8位的数据时,可重新typedef新的占8位的类型为int8,而所有的用int8定义的8为类型数不用再 ...
- 手机端获取用户详细地理位置(高德地图API)
项目开发需要获取用户详细的地理位置信息,使用了高德地图API接口 1,注册高德地图开发者账号获取开发者Key 2,页面调用 <script type="text/javascript& ...
- .NET平台机器学习资源汇总,有你想要的么?(转)
出处:http://www.cnblogs.com/asxinyu/p/4422050.html 阅读目录 1.开源综合类 2.开源.NET平台非综合类 3.其他资源与技术博客 4.我的100篇博客之 ...
- IndexedDB:浏览器里内置的数据库(转)
出处;http://www.webhek.com/indexeddb/ IndexedDB是HTML5规范里新出现的浏览器里内置的数据库.对于在浏览器里存储数据,你可以使用cookies或local ...
- Servlet.service() for servlet UserServlet threw exception java.lang.NullPointerException 空指针异常
错误付现: 严重: Servlet.service() for servlet UserServlet threw exceptionjava.lang.NullPointerException at ...
- Lucene4:获取中文分词结果,根据文本计算boost
1. 要求 环境: Lucene 4.1版本/IKAnalyzer 2012 FF版本/mmseg4j 1.9版本 实现功能: 1).给定输入文本,获取中文拆分词结果:2).给定输入文本,对该文本按一 ...
- ASP.NET WebApi总结之自定义权限验证
在.NET中有两个AuthorizeAttribute类, 一个定义在System.Web.Http命名空间下 #region 程序集 System.Web.Http, Version=5.2.3.0 ...