This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ArchAngel075's profile picture

[SOLVED]Assigned value to a i in table, value assigns to all dupes? [unsure on title]]

Started by ArchAngel075, 13 June 2013 - 09:29 AM
ArchAngel075 #1
Posted 13 June 2013 - 11:29 AM
So I have this :


term.clear()
term.setCursorPos(1,1)

World = {
Tile = {
}
}

TileM = {
redBrick = {signedName = "redBrick",script = "#none",Xpos = 0,Ypos = 0,graphic = "=",color = colors.red,pixel = "#none",Solid = true},
Grass = {signedName = "Grass",script = "#none",Xpos = 0,Ypos = 0,graphic = "#",color = colors.red,pixel = "#none",Solid = true},
}

_table = TileM.redBrick
table.insert(World.Tile,1,_table)
_table = TileM.Grass
World.Tile[1].Xpos = 1
table.insert(World.Tile,2,_table)
World.Tile[2].Xpos = 2
_table = TileM.Grass
table.insert(World.Tile,3,_table)
World.Tile[3].Xpos = 3
_table = TileM.redBrick
table.insert(World.Tile,4,_table)
World.Tile[4].Xpos = 4

for k,v in pairs(World.Tile) do
print( k .. " : " .. v.signedName .. " X : " .. v.Xpos)
end


First i make sure that World has a table in it called Tile, then I made a table TileM with pre-added tables.
Now i want to insert a "Tile" inside of World.Tile, so i used table.insert(etc)

Now i wish to edit the Xpos of each object i added, so i used World.Tile.Xpos = Value
I could have used this in place of the index where ever a integer index is given : #World.Tile.

Now my issue is when i have the values printed out, the Xpos's are the same for "Tiles" that are named the same (in this case numbers 1 and 4 are same, 2 and 3 are same (1+4 are redBrick,2+3 are Grass)

So i will get this printed :
1 : redBrick X : 4
2 : Grass X : 3
3 : Grass X : 3
4 : redBrick X : 4

where instead i want this output :
1 : redBrick X : 1
2 : Grass X : 2
3 : Grass X : 3
4 : redBrick X : 4
—–

What is causing this ? As i need a way to insert tiles into World.Tile, and table.insert ALLWAYS does this for like named Tiles even though those tiles are of a different index…
GopherAtl #2
Posted 13 June 2013 - 11:53 AM
Tables are copied by reference rather than value in lua.

What that means…

When you define a table, with {}, a new table is created. The variable you assign this to, ex, "myTable={}", myTable is not set to the actual table, it is set to refer to that table. So when you assign that to another object, you are not copying the table, just making another reference to the same table. This is what you are seeing.

you'll have to actually make a new table for each separate copy you want to get the behavior you were expecting. You can do a basic copy of a table like this:


local function copyTable(t)
  local copy={}
  --for every key/value pair in the original table...
  for k,v in pairs(t) do
    --if this value is also a table, need to copy it as well
    if type(v)=="table" then
	  copy[k]=copyTable(v)
    else --everything else can just be assigned over
      copy[k]=v
    end
  end
  return copy
end

local myTable={1,2,3}
local sameTable=myTable
local tableCopy=copyTable(myTable)

after this, modifying myTable will also modify sameTable, and vice-versa
but tableCopy will not be affected, being a new table that simply holds the same values.
PixelToast #3
Posted 13 June 2013 - 11:58 AM
-snip-
ninjad :ph34r:/>

table.insert(World.Tile,copyTable(TileM.redBrick))
World.Tile[1].Xpos = 1
table.insert(World.Tile,copyTable(TileM.Grass))
World.Tile[2].Xpos = 2
table.insert(World.Tile,copyTable(TileM.Grass))
World.Tile[3].Xpos = 3
table.insert(World.Tile,copyTable(TileM.redBrick))
World.Tile[4].Xpos = 4
ArchAngel075 #4
Posted 13 June 2013 - 12:07 PM
Ah thanks, +1 to both of you :D/>