日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

lua中的坑

發布時間:2023/12/20 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lua中的坑 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  在工作中使用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 ) dofor pid,info in pairs( hurt_info ) dolocal _info = {}_info.ty = ty_info.pid = pid_info.hurt = info.hurt_info.times = info.timestable.insert( player_hurt,_info )endendtable.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就可以使用配置了。轉出來的配置表通常是這樣的:

local t = {[1] = {['id'] = 1,['icon'] = 1001,['name'] = '默認頭像框',['description'] = '初始贈送',['type_id'] = 1,['sort'] = 1,},[2] = {['id'] = 2,['icon'] = 1002,['name'] = '10級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 10,['sort'] = 2,},[3] = {['id'] = 3,['icon'] = 1003,['name'] = '20級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 20,['sort'] = 3,},[4] = {['id'] = 4,['icon'] = 1004,['name'] = '30級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 30,['sort'] = 4,},[5] = {['id'] = 5,['icon'] = 1005,['name'] = '40級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 40,['sort'] = 5,},[6] = {['id'] = 6,['icon'] = 1006,['name'] = '50級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 50,['sort'] = 6,},[7] = {['id'] = 7,['icon'] = 1007,['name'] = '60級頭像框',['description'] = '達到$1級即可獲得',['type_id'] = 2,['num'] = 60,['sort'] = 7,},} View Code

在某些功能中,是需要將id從小到大遍歷的。由于策劃配置時總是嚴格按小到大的,轉換出來的配置表key值也是從小到大的。然而,當我們require這個table時,順序卻是亂的。即

for k,v in pairs( t ) do
print( k,v )
end

輸出:

Program 'lua.exe' started in 'D:\work_code\server' (pid: 8044). 2 table: 0x003d8ff8 5 table: 0x003d92c8 3 table: 0x003d90e8 7 table: 0x003d8a58 1 table: 0x003da560 4 table: 0x003d91d8 6 table: 0x003d92f0 Program completed in 0.03 seconds (pid: 8044).

這樣的代碼,第一個取得的id并不一定是1。也就是說,lua虛擬機在編譯這個文件時,里面的元素并不是按順序的,即使key值是int型并且按從小到大并嚴格自增。

轉載于:https://www.cnblogs.com/coding-my-life/p/5223533.html

總結

以上是生活随笔為你收集整理的lua中的坑的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。