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

Block IDs for my game get mixed up.

Started by jerp2042, 23 February 2013 - 12:59 PM
jerp2042 #1
Posted 23 February 2013 - 01:59 PM
Title: Block IDs for my game get mixed up.

So I set up my game so it sets a block ID in a table in another table. It does this using:

database[x][y]
With database being the table.

I made it so the blocks write the IDs on themselves.
http://tinyurl.com/ccblockidhelp1
And I made it so whenever I click on a block it gets the ID of the block and reprints it.
But it never reprints the right ID.
http://tinyurl.com/ccblockidhelp2
Can some help me, tell me what I'm doing wrong or revamp the code or something. :(/>

Spoiler

tw,th = term.getSize()
database = {}
for fx=1,tw,1 do
table.insert(database,fx,{})
for fy=2,th,1 do
  table.insert(database[fx],fy,0)
end
end
function block(x,y,id)
table.insert(database[x],y,id)
if id == 0 then
  -- Air
  term.setBackgroundColor(colors.black)
  term.setCursorPos(x,y)
  write(database[x][y])
elseif id == 1 then
  -- Stone
  term.setBackgroundColor(colors.gray)
  term.setCursorPos(x,y)
  write(database[x][y])
elseif id == 2 then
  -- Dirt
  term.setBackgroundColor(colors.brown)
  term.setCursorPos(x,y)
  write(database[x][y])
elseif id == 3 then
  -- Grass
  term.setBackgroundColor(colors.green)
  term.setCursorPos(x,y)
  write(database[x][y])
end
end
function getBlockID(x,y)
return database[x][y]
end
-- Initialize
term.clear()
-- Generation
genheight = th / 2
for gencolumn=1,tw,1 do
genheight = genheight + (math.random(1,3) - 2)
for genblock=tw,genheight,-1 do
  block(gencolumn,genblock,1)
  block(gencolumn,genblock - 1,2)
  block(gencolumn,genblock - 2,2)
  block(gencolumn,genblock - 3,2)
  block(gencolumn,genblock - 4,3)
end
end
-- Player
while true do
event, button, x, y = os.pullEvent("mouse_click")
term.setCursorPos(x,y)
term.setTextColor(colors.red)
term.setBackgroundColor(colors.lightBlue)
write(getBlockID(x,y))
end

I'm sorry I don't use comments much. That'll probably make it a whole lot easier on you guys…
Lyqyd #2
Posted 23 February 2013 - 05:35 PM
Split into new topic.
ChunLing #3
Posted 23 February 2013 - 05:49 PM
You use table.insert() in your block function. This is unnecessary and will shift blocks about if it is called on an existing block (given that you pre-generate the array, that occurs every time you call it), just use: database[x][y] = id.
jerp2042 #4
Posted 24 February 2013 - 04:20 AM
Thanks for helping! It worked! :D/>