码迷,mamicode.com
首页 > 其他好文 > 详细

lua中的坑

时间:2016-02-27 21:56:53      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

  在工作中使用lua也有一年了,代码也写了不少,踩过不少坑,这里记录一下。

  • table.sort

  table.sort是lua自带的排序函数,数据量小时,也还是不错的。不过要注意你传入的compare函数。例如:

local tb = { 9,8,3,777,0,36548,556,0 }

table.sort( tb,function(a,b) return a>=b end )

上面的代码够简单了,但是你运行起来,却是报错了。

Program starting as "D:\programs\ZeroBraneStudio\bin\lua53.exe" -e "io.stdout:setvbuf(no)" "D:\work_code\lua\Test\Test.lua".
Program lua53.exe started in D:\work_code\server (pid: 6572).
D:\programs\ZeroBraneStudio\bin\lua53.exe: D:\work_code\lua\Test\Test.lua:77: invalid order function for sorting
stack traceback:
[C]: in function sort
D:\work_code\lua\Test\Test.lua:77: in main chunk
[C]: in ?
Program completed in 0.04 seconds (pid: 6572).

原因在于compare函数中不能出现等于,即a>b或a<b都OK,但不能是a>=b或a<=b。如果使用了=,当数组中出现两个权重一样的元素时,就会报错。具体原因你可以看table.sort的官方文档,有句话:

If comp is given, then it must be a function that receives two list elements and returns true when the first element must come before the second in the final order (so that, after the sort, i < j implies not comp(list[j],list[i]))

如果是报invalid order function for sorting也就罢了,网上查一下就能找到答案。然而我在项目中的报错却比较奇怪:

function Society_city:do_player_hurt_sort()
    local player_hurt = {}
    for ty,hurt_info in pairs( self.player_hurt ) do
        for pid,info in pairs( hurt_info ) do
            local _info = {}
            _info.ty = ty
            _info.pid = pid
            _info.hurt = info.hurt
            _info.times = info.times

            table.insert( player_hurt,_info )
        end
    end

    table.sort( player_hurt,function(a,b) return a.hurt >= b.hurt end )

    return player_hurt
end   

报错信息为:

society_city.lua:679: attempt to index local a (a nil value)

我一直以为是数组中加入了一个nil,但查了半天,原来是>=的原因。

  • table编译

在我们的项目中,策划将配置填到excel表,然后用工具将excel表批量转为lua表作为配置。这样在lua中直接require就可以使用配置了。转出来的配置表通常是这样的:

return 
{
    [1] = 
    {
        [id] = 1,
        [atk] = 999,
    },

    [2] = 
    {
        [id] = 2,
        [atk] = 888,
    },
}

在某些功能中,是需要将id从小到大遍历的。由于策划配置时总是严格按小到大的,转换出来的配置表key值也是从小到大的。然而,当我们require这个table时,顺序却是乱的。即

local tb = require "config_weapon"

for id,info in pairs( tb )do
    ...
end

这样的代码,第一个取得的id并不一定是1。也就是说,lua虚拟机在编译这个文件时,里面的元素并不是按顺序的,即使key值是int型并且按从小到大并严格自增。

lua中的坑

标签:

原文地址:http://www.cnblogs.com/coding-my-life/p/5223533.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!