Add, delete, and modify data in LuaTable, LuaArrayTable, and LuaDictTable in Unity C#

introduce

Adding, deleting, modifying, and querying Lua tables is a common table operation in Lua and C#. Here we mainly compare how to add, delete, modify, and query tables in Lua and C# respectively.

Lua table

lua table initialization

  1. mytable = {}

lua remove reference

  1. mytable = nil

Add data to table in lua

  1. Insert an xx data into the index position in xxtable table.insert(xxtab,index,xx)

  2. Insert an xx data into the end position of xxtable table.insert(xxtab,xx)

  3. Add a yytab table data to the end of xxtabtable.insert(xxtab,yytab)

  4. Modify the index position value in xxtab to xxx (no value is equivalent to adding data)xxtab[index] = xxx

Remove data from table in Lua

  1. Remove specified index data in the table in lua table.remove(xxtab,index)
  2. Remove the last data in the table in lua table.remove(xxtab)

Connect data in lua table

  1. lua connection datatable.concat(xxtab) For example {"a","b","c",5,"d "} Output string type "abc5d"
  2. Use x symbol to connect data in the tabletable.concat(xxtab,“x”) For example {“a”,“b”, "c",5,"d"} Output string type "axbxcx5xd"
  3. Use x symbol to connect the data from 2 to 4 in the tabletable.concat(xxtab,“x”,2,4) For example { "a","b","c",5,"d"} Output string type "bxcx5"

Sorting data in Lua table

  1. Lua table default sorting table.sort(table)
  2. lua table custom function(a,b) conditional sorting table.sort (table, funtion(a,b))

Get the length of lua table

  1. Get the length of the table in lua #table

Get the maximum value in the table

  1. Get the maximum value in the xxtab table in luatable.maxn(xxtab) For example, {1,2,3,4,7} Output 7

UnityC#中LuaTable

  1. LuaTable GetMetaTable()
  2. 转成LuaArrayTableLuaArrayTable ToArrayTable()
  3. 转成LuaDictTable LuaDictTable ToDictTable()
  4. 转成LuaDictTable<K, V> LuaDictTable<K, V> ToDictTable<K, V>()
  5. 转成object[] object[] ToArray()
  6. Print table memory address string ToString()
  7. Modify value and get value
    Modify: tab[index] = xxx or tab[“xxx”] = xxx
    Modify: table.SetTable< ; T >(string,T)
    Modify: table.RawSet<K, V>(K,V)
    Modify: RawSetIndex< T >( int, T)
    Get: T RawGetIndex< T >(int)
    Get: tab[index] or tab["xxx"]
    Get: T table.GetTable< T >(string)
    Get: V table.RawGet<K, V>(K)
    The lua table is as follows
    Insert image description here
    Modify and copy the code as follows:
    Insert image description here
    Print as follows:
    Insert image description here
  8. Get length tab.Length
  9. Func method in LuaTable
    Get method GetLuaFunction("key")
    Get method RawGetLuaFunction("key")
    The execution method has no return value and no parameters func1.Call()
    The execution method has no return value func1.Call< T >(T) supports multiple parameters and will not be written in detail here Print as follows: The code is as follows: The lua table is as follows: The execution method has a return value and has parameters R func1.Invoke<T , R>(T ) Support for multiple parameters will not be written in detail here
    The execution method has a return value and no parameters R func1.Invoke< R >()


    Insert image description here

    Insert image description here

    Insert image description here

UnityC#中LuaArrayTable、LuaDictTable、LuaDictTable<K,V>

They all inherit from IEnumerable< T > Equivalent to the usage of Array Dictionary List
The following method can be used for traversal

	var itor = tab.GetEnumerator();
    while (itor.MoveNext())
    {
    
    
        Debug.LogError("itor.Current.Key = " + itor.Current.Key + " ,itor.Current.Value = " + itor.Current.Value);
    }

    itor.Dispose();

It can also assist LuaTable to convert into corresponding List and Dictionary<K,V>

Guess you like

Origin blog.csdn.net/qq_42194657/article/details/134055008