Hey people who write a lot of Lua code in Roblox, is there a “standard library” for doing common operations like cloning a table that you use? Maybe hidden in one of those core scripts that loads when the game runs?
It’s pretty amazing to me that in a language where everything is a table there is no support for copying tables.
I did a quick Google search and ended up on the following article which should have everything you need: lua-users wiki: Copy Table
Coincidentally the article also provides an answer to your question about why Lua has no native support for copying tables:
A generic table.copy function is not guaranteed to suit all use-cases, as there are many different aspects which must be selected for the specific situation. For example: should metatables be shared or copied? Should we check userdata for a __copy metamethod?
The Deep copy implementation should cover all cases.
There are probably extended Lua libraries out there, though the normal approach to this problem (for me that is) is to just iterate through one table and put the values in a new table.
function Table_Clone(t)
local r = {}
for k, v in pairs(t) do
r[k] = v
end
return r
end
That doesn’t copy a table. That makes one filled table and one empty table, and then replaces the empty table with a reference to the filled table, deleting the empty table.
I don’t want to copy that function into every script that wants to use it, or create a new module script containing that function if there is already a “standard library” require that Roblox developers commonly use.
I.E. I DONT WANT TO BUILD MY OWN STANDARD LIBRARY
Put another way, am I missing something or do I really have to implement this myself?
Someone recently made a huge library of extensions. Maybe look into that?
Other than looking at things like this or making your own, I don’t know what to tell you.
Lua was deliberately designed as a simple stripped-down language with a minimum set of standard libraries, which is very different from the design philosophy behind C# and the .NET Framework.
Check out the CorePackages folder in your current Roblox install - this is where the relevant core libraries are (%LOCALAPPDATA%/Roblox/Versions/version-<hash>/ExtraContent/LuaPackages).
Regarding table operations, Cryo is what you’re looking for. You’ll have to follow a few requires through to find the source files for the library itself.