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

Coordinate efficiency

Started by Thefdjurt, 05 August 2016 - 09:36 PM
Thefdjurt #1
Posted 05 August 2016 - 11:36 PM
I am working on a little game so I am not overly concerned about efficiency, but whatever.

The game is "tile" based and each move does an analysis on the surrounding tiles
To get tiles I am currently doing

function getTile(x,y)
  return data[(y-1)*width+x-1]
end
I realize it is much easier to get tiles if each y-coord had their own table within data, but is it really more efficient in terms of computation. Thank you
Edited on 05 August 2016 - 09:41 PM
Lignum #2
Posted 05 August 2016 - 11:40 PM
I realize it is much easier to get tiles if each y-coord had their own table within data, but is it really more efficient in terms of computation. Thank you

While in some languages this is more efficient, in Lua it certainly isn't, since that'd require two table lookups instead of one; and table lookups are more expensive than doing a simple calculation! So you're better off sticking with what you have right now.
CrazedProgrammer #3
Posted 06 August 2016 - 12:22 AM
I agree with Lignum, there's a reason my programs use this format for 2D arrays.