Good day, I was wondering why is my table being altered even though I set it as a use of reference for other table. Sample:
tableCopy.lua
local M = {} local origTable = loadTableFromJson M.copyTable = function() local otherTable = origTable return otherTable end return M
main.lua
local tableCopy = require("tableCopy") local myTable = {} myTable = tableCopy.copyTable() myTable.hp = myTable.hp - 1 -- Output is from 10 to 9 local newTable = tableCopy.copyTable() newTable.hp = newTable.hp - 1 -- Output is 8, the hp still retains
as you can see, the hp still continues even though, in my copyTable.lua I’m just copying the table. I already solved it by just reReading the JSON again, just asking why does this happen?
thanks in advance
When you assign otherTable = origTable, you aren’t making a copy. You are storing a reference to the table named ‘origTable’ in otherTable.
You need a function which creates a brand new object with the same properties as the old.
https://forums.coronalabs.com/topic/27482-copy-not-direct-reference-of-table/
Depending on the size and complexity of your table, this can be a fairly slow process. It might be quicker to just re-read the data.
okay thanks, i already did the re-read the data. I was wondering what was happening, thank you
Don’t reload the JSON. That is slow and if you do it a lot it will affect the game performance as well as burn battery life.
Make a copy of the table.
If you have SSK2, it has several copy features:
- table.shallowCopy() - Create new table copying first level of source table. Copies of sub-levels are references.
- table.shallowStripCopy() - Same, but do not copy functions or userdata (in preparation for serialization).
-
table.deepCopy() - Traverse entire table and create complete copy of all data. Cannot handle cyclic tables.
- table.deepStripCopy() - Same, but with serialization prep.
Note: You can also find table copying examples by searching for “Lua Table Copy” here is one example (Rosetta Code is one of my favorite sites):
https://rosettacode.org/wiki/Deepcopy#Lua
When you assign otherTable = origTable, you aren’t making a copy. You are storing a reference to the table named ‘origTable’ in otherTable.
You need a function which creates a brand new object with the same properties as the old.
https://forums.coronalabs.com/topic/27482-copy-not-direct-reference-of-table/
Depending on the size and complexity of your table, this can be a fairly slow process. It might be quicker to just re-read the data.
okay thanks, i already did the re-read the data. I was wondering what was happening, thank you
Don’t reload the JSON. That is slow and if you do it a lot it will affect the game performance as well as burn battery life.
Make a copy of the table.
If you have SSK2, it has several copy features:
- table.shallowCopy() - Create new table copying first level of source table. Copies of sub-levels are references.
- table.shallowStripCopy() - Same, but do not copy functions or userdata (in preparation for serialization).
-
table.deepCopy() - Traverse entire table and create complete copy of all data. Cannot handle cyclic tables.
- table.deepStripCopy() - Same, but with serialization prep.
Note: You can also find table copying examples by searching for “Lua Table Copy” here is one example (Rosetta Code is one of my favorite sites):
https://rosettacode.org/wiki/Deepcopy#Lua