• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ Copy Table in right way

zxmatzx

Advanced OT User
Joined
Dec 1, 2010
Messages
312
Solutions
27
Reaction score
155
Location
Brazil
GitHub
Mateuso8
Hello,
I realized that if i copy table1, to another table2 field, make changes in table2, table1 will be changed too. I had same problem using C# sometime ago. I want to know WHY its happen and if have some way to avoid that(im using a function to copy that is avoiding it, just wanting to know if have another way).

Ex:
Lua:
BASE_INFOS = {
    [1] = {
        name = "adam",
        hp = 100,
        mp = 50,
        atk = 5,
    },
    [2] = {
        name = "jack",
        hp = 150,
        mp = 10,
        atk = 50,
    },
    [3] = {
        name = "john",
        hp = 1000,
        mp = 500,
        atk = 50,
    },
    [4] = {
        name = "rick",
        hp = 1,
        mp = 5,
        atk = 5,
    },
}

function getWrongWayJackInfo()
    local newInfos = {
        nickname = "the ripper",
        alive = false,
        baseStats = BASE_INFOS[2]
    }
    --newInfos.baseStats = {name = "jack", hp = 150, mp = 10, atk = 50}
    --BASE_INFOS[2] = {name = "jack", hp = 150, mp = 10, atk = 50}
    newInfos.baseStats.def = 20
    --newInfos.baseStats = {name = "jack", hp = 150, mp = 10, atk = 50, def = 50}
    --BASE_INFOS[2] = {name = "jack", hp = 150, mp = 10, atk = 50, def = 50}
end

function getRightWayJackInfo()
    local newInfos = {
        nickname = "the ripper",
        alive = false,
        baseStats = tableDeepCopy(BASE_INFOS[2])
    }
    --newInfos.baseStats = {name = "jack", hp = 150, mp = 10, atk = 50}
    --BASE_INFOS[2] = {name = "jack", hp = 150, mp = 10, atk = 50}
    newInfos.baseStats.def = 20
    --newInfos.baseStats = {name = "jack", hp = 150, mp = 10, atk = 50, def = 50}
    --BASE_INFOS[2] = {name = "jack", hp = 150, mp = 10, atk = 50}
end

function tableDeepCopy(original)
    local copy = {}
    for k, v in pairs(original) do
        if type(v) == table then
            v = tableDeepCopy(v)
        end
        copy[k] = v
    end
    return copy
end
Why its happen? Have another way to solve this? If yes, what should i use?(and why).
Thanks.
 
Last edited:
Solution
Tables are references, meaning regardless of variable name, they all refer to 1 singular table instead of copying the value, this is the same for functions.
If you want to copy a table and edit it, use this:
Lua:
function table.copy(t)
    local ret = {}
    for k, v in pairs(t) do
        ret[k] = v
    end
    return ret
end

Usage:
Lua:
local t = {1, 2, 3}
local t2 = table.copy(t)

t2[1] = 500 -- t2 = {500, 2, 3}, t = {1, 2, 3}

Edit: nevermind you're already copying the table. To answer your question, there is no other way, you must copy it to avoid changing references.
Tables are references, meaning regardless of variable name, they all refer to 1 singular table instead of copying the value, this is the same for functions.
If you want to copy a table and edit it, use this:
Lua:
function table.copy(t)
    local ret = {}
    for k, v in pairs(t) do
        ret[k] = v
    end
    return ret
end

Usage:
Lua:
local t = {1, 2, 3}
local t2 = table.copy(t)

t2[1] = 500 -- t2 = {500, 2, 3}, t = {1, 2, 3}

Edit: nevermind you're already copying the table. To answer your question, there is no other way, you must copy it to avoid changing references.
 
Solution
Tables are references, meaning regardless of variable name, they all refer to 1 singular table instead of copying the value, this is the same for functions.
If you want to copy a table and edit it, use this:
Lua:
function table.copy(t)
    local ret = {}
    for k, v in pairs(t) do
        ret[k] = v
    end
    return ret
end

Usage:
Lua:
local t = {1, 2, 3}
local t2 = table.copy(t)

t2[1] = 500 -- t2 = {500, 2, 3}, t = {1, 2, 3}

Edit: nevermind you're already copying the table. To answer your question, there is no other way, you must copy it to avoid changing references.
Oh, thanks for help. The function that u sended is the same that im using, but mine is recursive.
 
Back
Top