随笔分类 -  Lua

摘要://顶 -1 -2 -3 //顶 3 2 1 #include #include extern "C"{ #include #include #include } #pragma comment(lib,"lua.lib") void stackDump(lua_State* L) { int i; int top = lua_gett... 阅读全文
posted @ 2016-09-30 00:49 zzyoucan 阅读(512) 评论(0) 推荐(0) 编辑
摘要:#include #include extern "C"{ #include #include #include } #pragma comment(lib,"lua.lib") int main() { char buff[256]; int error; lua_State* L = luaL_newstate(); lua... 阅读全文
posted @ 2016-09-28 00:09 zzyoucan 阅读(159) 评论(0) 推荐(0) 编辑
摘要:Window = {} Window.prototype = {x = 0, y = 0, width = 70, height = 100} Window.mt = {} function Window.new(o) setmetatable(o, Window.mt) return o end --[[Window.mt.__index = function (table... 阅读全文
posted @ 2016-09-24 22:06 zzyoucan 阅读(128) 评论(0) 推荐(0) 编辑
摘要:Set = {} Set.mt = {} function Set.new(t) local set = {} setmetatable(set, Set.mt) for _, l in ipairs(t) do set[l] = true end return set end function Set.union(a, b) local res = S... 阅读全文
posted @ 2016-09-24 00:33 zzyoucan 阅读(190) 评论(0) 推荐(0) 编辑
摘要:function list_iter(t) local i = 0 local n = table.getn(t) return function() i = i + 1 if i <= n then return t[i] end end end t = {10, 20, 30} iter = list_iter(t) whil... 阅读全文
posted @ 2016-09-21 00:01 zzyoucan 阅读(220) 评论(0) 推荐(0) 编辑
摘要:--匿名函数使用upvalue i保存他的计数, 闭包是一个函数加上它可以正确访问的upvalues function newCounter() local i = 0 return function() i = i + 1 return i end end c1 = newCounter() print(c1()) print(c1()... 阅读全文
posted @ 2016-09-19 00:05 zzyoucan 阅读(134) 评论(0) 推荐(0) 编辑
摘要:关于项目中lua任务(某些没弄懂,但lua上耗费时间有点长了不看了)这段时间看了lua语法和项目中lua应用1.在lua中注册c库,在lua5.2中好像都是注册c库,相当于在lua中定义一个table,key是函数名字,value是函数指针m_spLuaState->RegLib("game", f... 阅读全文
posted @ 2015-03-15 23:16 zzyoucan 阅读(646) 评论(0) 推荐(0) 编辑
摘要:--lua仿单继承Account = { balance = 0}--对于成员变量,第一此访问要使用元表中的,在第一次也赋值到自己的域中了--将不涉及到__index了function Account:new(o) o = o or {} --setmetatable看到后面,忘了这个啥意思了... 阅读全文
posted @ 2015-03-11 22:22 zzyoucan 阅读(195) 评论(0) 推荐(0) 编辑
摘要:--lua仿单继承Account = { balance = 0}function Account:new(o) o = o or {} setmetatable(o, self)--Account表本身作为o的metatable self.__index = self--自己作为自己的原型 ... 阅读全文
posted @ 2015-03-10 13:35 zzyoucan 阅读(184) 评论(0) 推荐(0) 编辑
摘要:Account = { balance = 0}function Account:deposit(v) self.balance = self.balance + vendfunction Account:new(o) o = o or {} setmetatable(o, self)--A... 阅读全文
posted @ 2015-03-10 10:56 zzyoucan 阅读(135) 评论(0) 推荐(0) 编辑
摘要:Account ={ balance = 0, withdraw = function(self, v) self.balance = self.balance - v end}--:操作符隐藏了self或者this参数,操作方便function Account:deposit(v) ... 阅读全文
posted @ 2015-03-10 00:39 zzyoucan 阅读(136) 评论(0) 推荐(0) 编辑
摘要:--sort中的匿名函数中的grades称为外部局部变量,或者upvalue--函数内部定义的函数可以访问函数的变量--这个就是简单的闭包--function sortbygrade (names, grades)-- table.sort(names, function(n1, n2)-- ... 阅读全文
posted @ 2015-03-09 20:50 zzyoucan 阅读(117) 评论(0) 推荐(0) 编辑
摘要:Set = {}Set.mt = {}--定义普通的表作为元表,为了避免命名污染直接放在Set内部function Set.new(t) local set = {} setmetatable(set, Set.mt)--一组相关的表共享一个metatable(通过这个可以描述他们共同的... 阅读全文
posted @ 2015-03-09 15:44 zzyoucan 阅读(267) 评论(0) 推荐(0) 编辑
摘要:#include #include #define MAX_COLOR 255 extern "C"{#include "lua-5.2.2/src/lauxlib.h"#include "lua-5.2.2/src/lualib.h"#include "lua-5.2.2/src/ls... 阅读全文
posted @ 2015-03-09 11:42 zzyoucan 阅读(1985) 评论(0) 推荐(0) 编辑
摘要:#include #define MAX_COLOR 255 extern "C"{#include "lua-5.2.2/src/lauxlib.h"#include "lua-5.2.2/src/lualib.h"#include "lua-5.2.2/src/lstate.h"}i... 阅读全文
posted @ 2015-03-09 00:05 zzyoucan 阅读(6218) 评论(0) 推荐(0) 编辑
摘要:#include extern "C"{#include "lua-5.2.2/src/lauxlib.h"#include "lua-5.2.2/src/lualib.h"#include "lua-5.2.2/src/lstate.h"}//lua与c交互栈的索引,假如栈中有5个元素//5 -1... 阅读全文
posted @ 2015-03-08 19:56 zzyoucan 阅读(729) 评论(1) 推荐(0) 编辑
摘要:#include #include extern "C"{#include "lua-5.2.2/src/lua.h"#include "lua-5.2.2/src/lauxlib.h"#include "lua-5.2.2/src/lualib.h"}//定义库函数static int l_dir... 阅读全文
posted @ 2015-03-02 14:51 zzyoucan 阅读(569) 评论(0) 推荐(0) 编辑
摘要:#include #include extern "C"{/*头文件lua.h定义了Lua提供的基础函数,包括创建Lua环境、调用Lua函数、读写Lua环境中全局变量,以及注册供Lua调用的新函数等等*/#include "lua-5.2.2/src/lua.h"/*头文件lauxlib.h定义了辅... 阅读全文
posted @ 2015-03-02 10:58 zzyoucan 阅读(4398) 评论(0) 推荐(0) 编辑
摘要:lua 中pairs 和 ipairs区别标准库提供了集中迭代器,包括迭代文件每行的(io.lines),迭代table元素的(pairs),迭代数组元素的(ipairs),迭代字符串中单词的 (string.gmatch)等等。LUA手册中对与pairs,ipairs解释如下:ipairs (t)... 阅读全文
posted @ 2015-02-09 14:59 zzyoucan 阅读(821) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示