I've seen it here and there but never understood what it is used for, could someone explain what unpack() is for?
What is unpack used for?
Unpack is used for separating values in a table for example
1 | local Table = { "g" , "o" , "o" , "d" , "a" , "d" , "m" , "i" , "n" , "s" } -- This is my Table |
2 |
3 | print ( unpack (Table)) -- This unpacks the table and prints it as separate values |
Output
1 | g o o d a d m i n s |
This prints all the content of the table in one line
1 | local t = { "1" , "2" , "3" , "4" , "5" } |
2 | print ( unpack (t)) |
Above code prints out each element in 't', starting from index 1. A brief explanation would be as follows:
unpack is a special function in Lua which receives a specified table and returns the elements in that table in consecutive order, starting from index 1.