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:
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. :(/>
I'm sorry I don't use comments much. That'll probably make it a whole lot easier on you guys…
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…