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

Table problem

Started by KaBooMa, 06 January 2013 - 02:44 AM
KaBooMa #1
Posted 06 January 2013 - 03:44 AM
I have a paint program I am going to be using for TVs in our server and I seem to have a slight problem in the space function….Basically when you hit space, it does so,


if (not pic[frame+1]) then
  table.insert(pic,frame+1,pic[frame])
  frame = frame + 1
  frawFrame()
end

Now the problem is, it seems to set the new frame to be = to pic[frame], which is right, but when Edit the old frame, it sets the new frame to the current frame and if I edit the new frame, the old frame is the new frame. So it is sorta like, frame 1 is equal to frame 2 and frame 2 is equal to frame 1….Sorta weird and I was wondering if there might be a way to fix it. I think it has something to do with making it equal pic[frame]. I believe what it is doing is saying, alright, lets change pic[frame][x][y] to the new color, and it is not actually making it equal like I want. I could be totally wrong.
KaBooMa #2
Posted 06 January 2013 - 04:06 AM
Alright so I kinda went ahead and fixed up this problem. I still wonder, why does this happen exactly?
ChunLing #3
Posted 06 January 2013 - 04:11 AM
Your frames are themselves tables, right?

Assignment of a table to another variable (or table entry) only copies the reference to the table, so both end up sharing the data.

Lua tables

Of note,
A table is always anonymous. There is no fixed relationship between a variable that holds a table and the table itself:
a = {}
a["x"] = 10
b = a – `b' refers to the same table as `a'
print(b["x"]) –> 10
b["x"] = 20
print(a["x"]) –> 20
a = nil – now only `b' still refers to the table
b = nil – now there are no references left to the table
KaBooMa #4
Posted 06 January 2013 - 04:14 AM
Ya that is what I was trying to say just didn't know how I could put it. So when I link a table to be a table, it references it. I managed to fix this all up by doing

if (not pic[frame+1] and pic[frame]) then
  table.insert(pic,frame+1,{})
  for tx, _ in pairs(pic[frame]) do
	table.insert(pic[frame+1],tx{})
	for ty, c in pairs(pic[frame][tx]) do
	  table.insert(pic[frame+1][tx],ty,c)
	end
  end
  end
end