Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Could someone explain to me what unpack() does and what it is used for?

Asked by 5 years ago

I've seen it here and there but never understood what it is used for, could someone explain what unpack() is for?

2 answers

Log in to vote
0
Answered by
Prestory 1395 Moderation Voter
5 years ago
Edited 5 years ago

What is unpack used for?

Unpack is used for separating values in a table for example

local Table = {"g","o","o","d","a","d","m","i","n","s"} -- This is my Table

print(unpack(Table)) -- This unpacks the table and prints it as separate values

Output

g o o d a d m i n s

This prints all the content of the table in one line

0
They're called elements not "content" Theswagger66 54 — 5y
0
Actually the things inside the table are the tables 'content' smart kid Prestory 1395 — 5y
0
I can’t tell if you’re serious or not, first you downvote a correct answer then you refer to elements as “things” Theswagger66 54 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
local t = {"1","2","3","4","5"}
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.

Answer this question