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

[Closed]Metatable Help

Started by Tamtam18_2, 21 September 2015 - 06:34 AM
Tamtam18_2 #1
Posted 21 September 2015 - 08:34 AM
I want to convert the following block of code:


  if not blockData[x] then blockData[x] = {} end
  if not blockData[x][y] then blockData[x][y] = {} end
  if not blockData[x][y][z] then blockData[x][y][z] = {} end
  blockData[x][y][z]["name"] = data["name"]
  blockData[x][y][z]["metadata"] = data["metadata"]

into one line, or something more plesent:


  blockData = {
  [x]={
	[y]={
	  [z]={
		name=data["name"],
		metadata=data["metadata"]
}}}}

but I am confused as how to do this. Here's my attempt:


	blockData = {
	_index = if not [x] then
	  [x]={}
	  end
	  return [x] = x
	  }

What am i doing wrong?
Edited on 21 September 2015 - 09:35 AM
Lupus590 #2
Posted 21 September 2015 - 08:45 AM
The first three lines can be shortended

--#this does sacrifice readability in my opinion
blockData[x] = blockData[x] or {}
blockData[x][y] = blockData[x][y] or {}
blockData[x][y][z] = blockData[x][y][z] or {}

If the table called data is not going to be used for anything and contains only the stuff want then you could set blockData[x][y][z] to data.

Your second attempt creates a new table, only use { } when you want a new table. This is why it doesn't work, this also mean that your third attempt also has this problem. The third also has another problem, but I don't have time to explain it (off to uni) will edit when I'm there.

You may want to try reading this thread (http://www.computerc...o-optimization/).
Edited on 21 September 2015 - 06:50 AM
Tamtam18_2 #3
Posted 21 September 2015 - 09:14 AM
pastebin

your answer makes it a bit better, but its doing the same thing.

data is used to grab information from getBlockInfo. The first block of code in the first post creates a new xyz table to store the name and meta of the block being saved. This is necessary to save all blocks in a selected area.

expected result:

pastebin
Bomb Bloke #4
Posted 21 September 2015 - 10:50 AM
Why not:

if not blockData[x] then blockData[x] = {} end
if not blockData[x][y] then blockData[x][y] = {} end
blockData[x][y][z] = {name=data["name"], metadata=data["metadata"]}

Or, better yet, how about we just ditch the saveBlock(x,y,z) function completely, and change the loop down in saveArea(x,y,z, x2,y2,z2)?:

  print("Scanning Area...")
  blockData = {}
  for x = xStart, xEnd,fx do
    blockData[x] = {}
    for y = yStart, yEnd do
      blockData[x][y] = {}
      for z = zStart, zEnd, fz do
        blockData[x][y][z] = commands.getBlockInfo(x,y,z)
      end
    end
  end

your answer makes it a bit better, but its doing the same thing.

Doing what, exactly?
Tamtam18_2 #5
Posted 21 September 2015 - 11:35 AM
Or, better yet, how about we just ditch the saveBlock(x,y,z) function completely, and change the loop down in saveArea(x,y,z, x2,y2,z2)?:

  print("Scanning Area...")
  blockData = {}
  for x = xStart, xEnd,fx do
	blockData[x] = {}
	for y = yStart, yEnd do
	  blockData[x][y] = {}
	  for z = zStart, zEnd, fz do
		blockData[x][y][z] = commands.getBlockInfo(x,y,z)
	  end
	end
  end

Thanks again bomb bloke for shedding some light :D/>. That saved a whole lot of space.

Doing what, exactly?

first post 2nd block of code, not relevant now that I've been helped.
Edited on 21 September 2015 - 09:36 AM