Jump to content




Table problem


  • You cannot reply to this topic
3 replies to this topic

#1 KaBooMa

  • New Members
  • 29 posts

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.

#2 KaBooMa

  • New Members
  • 29 posts

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?

#3 ChunLing

  • Members
  • 2,027 posts

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,

Quote

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



#4 KaBooMa

  • New Members
  • 29 posts

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users