XLua 在Lua中遍歷C 的Dictionary

2021-08-21 12:44:32 字數 1308 閱讀 9094

前些日子在處理一段客戶端資料時遇到這樣乙個問題,嘗試了各種的迴圈遍歷,結果都是不行,後來發現這段資料是c#的dictionary。

我們在lua中可以通過獲取迭代器的方式來遍歷c#的dictionary。

廢話不多說,直接上**:

local playerinfo = cs.sg.playerdata.instance.infos        --c#的dictionary

local iter = playerinfo:getenumerator()        --獲取迭代器

while iter:movenext() do                           --只要movenext指標不為空,就一直迭代

local v = iter.current.value

dofuction(v)                                 --獲取到就可以進行操作了

end

後來,把這個提取了一下寫了個public的工具,用來將dictionary轉成table

commonfunction.dictotable = function(csharpdic)

local dic = {}

local index =1

local iter = csharpdic:getenumerator()

while iter:movenext() do

local v = iter.current.value

dic[index] = v

index = index + 1

endreturn dic

end

大功告成。

需要注意一點的是,在xlua中通過獲取迭代器的方式來遍歷c#的字典是很費的操作,最好在c#中加乙個靜態方法,在c#中將dictionary轉為陣列,然後再取資料。

public class tooools

return info;

}else

}}

這時候再取資料

local playerinfo = cs.sg.playerdata.instance.infos        --c#的dictionary

local info = cs.sg.tools.getdicvalue(playerinfo) --獲取到需要的資料

當然出於節約效能的考慮,還是在c#轉為陣列,然後在push進lua。

歡迎大佬吐槽.....

在lua中遍歷中文

必要知識 字元編碼 lua中for的原理 描述中有不對的地方望見諒 例項 local utf8changer local function chsize char ifnot char then print not char return 0elseif char 240then return 4e...

xLua學習總結(三) C 訪問lua中資料

1.將lua中基本資料對映到c 型別 lua中資料 a 1 str 小明 檔案編碼改為utf 8 isright true c int a env.global.get a string str env.global.get str bool isright env.global.get isrig...

XLua框架學習(三)C 訪問Lua中的全域性函式

function add print add endusing xlua luaenv luaenv newluaenv luaenv.dostring require csharpcalllua 需要先將lua指令碼載入進來 action add1 luaenv.global.get action...