CurseForge and Overwolf are joining forces!
Awesome More Information
  • 0

    posted a message on Variable referencing
    Quote from egingell
    You can't pass tables by value, but you can copy it first.
    -- newTable = copyTable(oldTable)
    local function copyTable = function(t)
        local ret = {}
        for k, v in pairs(t) do
            ret[k] = v
        end
        return ret
    end


    serious /necro but I'd hate to see someone burned by this--

    be careful using the above to clone a table. if you look closely, you'll see that you're only copying the first "level" of the table -- if the table contains sub-tables (i.e. if any "v" in that loop is, itself, a table reference) then you are again copying the *reference* and not cloning the sub-table. if you want to clone the entire table, all the way down through any nested sub-tables, you'd have to do it recursively, i.e.

    -- newTable = copyTable(oldTable)
    local function copyTable = function(t)
      local ret = {}
      for k, v in pairs(t) do
        if typeof(v) == "table" then
          ret[k] = copyTable(v)
        else
          ret[k] = v
        end
      end
      return ret
    end
    
    Posted in: Lua Code Discussion
  • To post a comment, please or register a new account.