LuaTable to C# list and dictionary Dictionary

LuaTable to C# list and dictionary Dictionaty

  • introduce
  • Create table test in lua
    • list table in lua
      • Convert table to List
      • Convert table to Dictionary
    • key value pair table
      • Convert table to Dictionary
    • Multi-type key-value pair table
      • Convert table to Dictionary
  • Summarize

Introduction

In the past, we basically converted from List or Dictionary in C# to luaTable. It was rare to convert LuaTable into List or Dictionary in C#. A few days ago, when I was doing a demand, I had to convert the data in lua into a collection in C#. C The article about converting #List or Dictionary into luaTable has been written before and you can refer to it directly.

Create table test in lua

list table in lua

Note that luatable data starts from 1, not from 0. The default Key here is 1, 2, 3, 4, 5, 6, 7, 8, 9

 --table in lua
    this.testList = {<!-- -->1,2,3,4,5,6,7,8,9}
\t
--C# Medium
//Get the lua table
    LuaTable table = LuaManager.lua.GetTable("UIMainCityCtrl.testList");
\t
//Print the length of the lua table
    Debug.LogError("table.Length = " + table.Length);

    //Convert to array for printing
    object[] tabArray = table.ToArray();
    for (int i = 0; i < tabArray.Length; i + + )
    {<!-- -->
        Debug.LogError("tabArray[" + i + "] = " + tabArray[i]);
    }
    
    //Use luaTable to print directly
    for (int i = 0; i < table.Length; i + + )
    {<!-- -->
        Debug.LogError("table[" + (i + 1).ToString() + "] = " + table[(i + 1)]);
    }
\t
//The above two printing results are the same. If you are using

The print looks like this:

Convert table to List

These two methods are essentially the same. LuaArrayTable can be understood as a layer of encapsulation of object[]
Method 1

 object[] obj = table.ToArray();
    List<object> arr1 = new List<object>(obj);

    #region print one

    var iter = arr1.GetEnumerator();
    while (iter.MoveNext())
    {<!-- -->
        Debug.LogError("iter.Current = " + iter.Current);
    }

    #endregion

    #region print two
        
    arr1.ForEach((item) => {<!-- -->
        Debug.LogError("item = " + item);
    });

    #endregion


Method 2
Here is just a slight change in the conversion method. I won’t explain it here. I just encapsulate it.

 //LuaArrayTable luaArray = table.ToArrayTable(); Another conversion method
        LuaArrayTable luaArray = new LuaArrayTable(table);

//The printing of LuaArrayTable is as follows
        var iter = luaArray.GetEnumerator();
        while (iter.MoveNext())
        {<!-- -->
            Debug.LogError("iter = " + iter.Current);
        }

        List<object> arr2 = new List<object>(luaArray);

//It can be simplified like this
List<object> arr2 = luaArray.ToList();

Note that LuaArrayTable is actually a layer of encapsulation of List, so the Key can only be Int

Convert table to Dictionary

 //Convert directly to Object, object type dictionary
LuaDictTable<object, object> luaArray = table.ToDictTable<object, object>();
//Dictionary<object, object> dic = luaArray.ToDictionary(k => k.Key, v => v.Value);
    Dictionary<object, object> dic = luaArray.ToDictionary();
\t
//Convert to dictionary of type Int, Object
LuaDictTable<int, object> luaArray = table.ToDictTable<int, object>();
    //Dictionary<int, object> dic = luaArray.ToDictionary(k => k.Key, v => v.Value);
    Dictionary<int, object> dic = luaArray.ToDictionary();

//Convert to int, dictionary of type int
LuaDictTable<int, int> luaArray = table.ToDictTable<int, int>();
//Dictionary<int, int> dic = luaArray.ToDictionary(k => k.Key, v => v.Value);
Dictionary<int, int> dic = luaArray.ToDictionary();
\t
//Convert to int, string type dictionary
LuaDictTable<int, string> luaArray = table.ToDictTable<int, string>();
//Dictionary<int, string> dic = luaArray.ToDictionary(k => k.Key, v => v.Value);
Dictionary<int, string> dic = luaArray.ToDictionary();

//Print code
var itor = dic.GetEnumerator();
    while (itor.MoveNext())
    {<!-- -->
        Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
    }
    itor.Dispose();

Print as follows

Key-value pair table

This kind of table of key-value pairs must not be converted into a list. Since it is a key-value pair, it can only be converted into a dictionary.

 --lua in
    --Dictionary represented by table
    this.testDic = {<!-- -->
        ["1"] = 50,
        ["a"] = 80,
        ["3"] = 120,
        ["b"] = 130
    }
\t
//C# in
//Another way to get the Lua table
    //LuaTable table = LuaManager.lua.GetTable("UIMainCityCtrl.testDic");
\t
//Print the length of the lua table
    Debug.LogError("table.Length = " + table.Length);

    LuaDictTable dic = table.ToDictTable();
    //Same as the conversion above
    //LuaDictTable<string, object> dic = table.ToDictTable<string, object>();
\t
//Convert to a dictionary of key-value pairs whose value is int
//LuaDictTable<string, int> dic = table.ToDictTable<string, int>();
\t
var itor = dic.GetEnumerator();
    while (itor.MoveNext())
    {<!-- -->
        Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
    }
    itor.Dispose();

The print looks like this:
One thing here is that the key-value pair LuaTable has no length, the length is 0

Convert table to Dictionary

How to convert to dictionary

 //string, object type dictionary
LuaDictTable dic = table.ToDictTable();
    Dictionary<string, object> unitydic = dic.ToDictionary(k=>k.Key.ToString(),v => v.Value);
\t
//LuaDictTable<string, object> dic = table.ToDictTable<string, object>();
//Dictionary<string, object> unitydic = dic.ToDictionary();

//string int type dictionary
    LuaDictTable<string, int> dic = table.ToDictTable<string, int>();
    Dictionary<string, int> unitydic = dic.ToDictionary();

//Print as follows
var itor = unitydic.GetEnumerator();
    while (itor.MoveNext())
    {<!-- -->
        Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
    }
    itor.Dispose();

Multi-type key-value pair table

Convert table to Dictionary

This kind of table of key-value pairs must not be converted into a list. Since it is a key-value pair, it can only be converted into a dictionary.

 --lua in
    --Dictionary represented by table
    this.testDic = {<!-- -->
        ["1"] = 1,
        [true] = 1,
        [false] = true,
        ["123"] = false,
        [5] = "heiheihei"
    }
\t
//C# in
//Another way to get the Lua table
    //LuaTable table = LuaManager.lua.GetTable("UIMainCityCtrl.testDic");
    LuaDictTable<object,object> dic = table.ToDictTable<object, object>();
    Dictionary<object, object> unitydic = dic.ToDictionary();
\t
var itor = unitydic.GetEnumerator();
    while (itor.MoveNext())
    {<!-- -->
        Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
    }
    itor.Dispose();

Summary

LuaTable, LuaArrayTable, and LuaDictTable in lua can actually be converted to List and Dictionary in Unity.
Later I will talk about how to add, delete, modify and query data in LuaTable, LuaArrayTable and LuaDictTable in C#.