Lua爬坑手记

0x00 爬坑说明书
最近编写游戏的客户端逻辑,部分以Lua为主,介于使用的Lua的经验尚浅,也就边写边看《Lua程序设计》。书中有些知识点真的是过目即忘,发现Lua还是有一些坑,下面权当作是Lua的爬坑笔记。

0x01 Lua的类型
最近一次在处理UGUI中ScrollView控件动态生成的对象时,利用动态生成的对象后缀来作为后续计算的索引。代码类似:

function ConvertGameObjectIndex2Param(go)
    local data_index = string.match(go, '%d')
    print('data_index:', data_index)        -- 1
end

ConvertGameObjectIndex2Param("obj1")

其中data_index是计算得出的索引,在后续的运算中却始终索引不到data_index在table中对应的值,由于data_index输出的log显示正常,起初并没有怀疑到这里。排查数据源确信没有问题后,我总记得在《Lua程序设计》中看到过一段作者列举table中各种索引使用的代码:
这里写图片描述
果然是纸上得来终觉浅,绝知此事要躬行啊。遂打印data_index的类型:

function ConvertGameObjectIndex2Param(go)
    local data_index = string.match(go, '%d')
    print('date_index:', data_index, type(data_index))  -- 1    string
    data_index = tonumber(data_index)
    print('date_index:', data_index, type(data_index))  -- 1    number
end

使用tonumber()转换为number类型后,问题解决。

0x01 你到底是什么类型?
上述代码段data_index第一次的类型是string,进行转换后data_index的类型变成了number。熟悉动态类型语言的coder对于此情此景应该已经见怪不怪了。而我等半截入土的古董从C/C++而来,总是感觉哪里不太对。明明声明的时候是string怎么后面还会变成number呢?
这是Lua灵活的地方,也是容易混淆的地方,用好了可以节省时间,用不好堪比C语言中的野指针。以至于我这几天在所有要通过number类型索引数据的地方,都会心惊胆战的先用tonumber()转了它再说。

0x02 table数据的读取
最初看到table,总是会执迷于去窥探“关联数组”的实现方式。而日常的使用中,却连最基本的使用方式都拎不清,真是“脸红”。不信你看下面这段代码输出什么呢?

local test_table = {}
index = 1
test_table[index] = "data_num"
test_table["index"] = "data_str"

print('test_table[index]', test_table[1])               -- data_num
print('test_table["index"]', test_table["index"])       -- data_str
print('test_table.index', test_table.index)             -- data_str

要注意test_table.index是与test_table[“index”]完全等价的。上面的是不是so easy?那么进阶版来了:

local test_table = {}
local num_index = 1
local str_index = "1"
test_table[num_index] = 'data_num'
test_table[str_index] = 'data_str'

print('test_table[num_index]', test_table[num_index])                       -- data_num
print('test_table[str_index]', test_table[str_index])                       -- data_str
print('test_table.num_index', test_table.num_index)                         -- nil
print('test_table.str_index', test_table.str_index)                         -- nil
print('test_table[tostring(num_index)]', test_table[tostring(num_index)])   -- data_str
print('test_table[tonumber(str_index)]', test_table[tonumber(str_index)])   -- data_num

print('test_table[1]', test_table[1])                                       -- data_num
print('test_table[01]', test_table[01])                                     -- data_num
print('test_table["01"]', test_table["01"])                                 -- nil

据说搞懂上面这一堆乱七八糟的东西可以召唤神龙!

0x03 自动转换之殇
Lua提供了数字与字符串之间的自动转换,对一个字符串进行算术运算时,Lua会将这个字符串转换成一个数字:

local temp = '10' + 1
print(temp, type(temp))     -- 11   number
temp = '10 + 1'
print(temp, type(temp))     -- 10 + 1   string

Lua中还有字符串连接操作符..,当..出现在数字后面时,需要额外添加一个空格,以免Lua将之与小数点混淆:

temp = 10 .. 20
print(temp, type(temp))     -- 1020     string

0x04 真亦假,假亦真
在Lua中将false与nil视为假,其他都为真。那么从C/C++过来搞事情的程序员最容易碰到什么问题呢?当然是数字0和空字符串拉。那怎么办呢?当然是选择原谅他!

local condition1 = true
local condition2 = 0
local condition3 = ""
local condition4 = false
local condition5 = nil

if condition1 then print('condition1:', condition1) end         -- true
if condition2 then print('condition2:', condition2) end         -- 0
if condition3 then print('condition3:', condition3) end
if not condition4 then print('condition4:', condition4) end     -- false
if not condition5 then print('condition5:', condition5) end     -- nil
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值