Table:copy and other standard operations

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.

8 Likes

I’ve seen those, but I’m not sure.

I’ve tried copying scripts before, they don’t work.

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.

4 Likes

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

I don’t see the need for Copying a table with such large functions @ForbiddenJ , correct me if I am wrong, but copying a table is as simple as this;

local Table1 = {"TableObject" , 1 , 2 , 3}
local Table2 = { }

Table2 = Table1

Unless you meant putting the objects from Table1 into Table2 without completely erasing Table2, then all these functions would make sense.

1 Like

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.

4 Likes

Yeah I know how to do it.

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?

3 Likes

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.

1 Like

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.

2 Likes

That is excellent, thanks.

… 30

2 Likes

i havent updated that for like 3 months

:grimacing:

But for the most part it should work. Will be looking into updating it a bit

cc @ForbiddenJ

1 Like