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

problems with tables

Started by roosterhat, 01 June 2014 - 06:16 PM
roosterhat #1
Posted 01 June 2014 - 08:16 PM
  • So im testing out pseudo object oriented programming. And im trying to make a program that displays various pages which you can add buttons to. So I made a method that prints out all of the page content but it thinks that page1 has a label in it so I throws an error saying “attempt to preform achromatic add on number and nil at line 152”. But I never added a label to page1 only to page2.
  • Another problem that is weird is that when I run the program for the first time after opening up minecraft it says “attempt to call nil at line 136” but then I comment out the contents in pages and run it then uncomment them and run it again, it runs normally.
  • And a third problem if the first one get solved is why does it print the same background for both pages?
Spoiler

content =
{
page1 =
{
  background = nil,
  visible = true,
  setBackground = function(path)
   background = paintutils.loadImage(path)
  end,
  getBackground = function()
   return background
  end,
  getPageContent = function()
   return pageContent
  end,
  setVisable = function(vis)
   visible = vis
  end,
  pageContent =
  {
   buttons = {},
   labels = {},
   images = {},
   getButtons = function()
	return content.page1.pageContent.buttons
   end,
   addButton = function(text,xpos,ypos,wid,high,c,tc,f)
	content.page1.pageContent.buttons[#content.page1.pageContent.buttons+1] =
		   {name = text,x = xpos,y = ypos
		   ,width = wid,height = high,color = c
		   ,textcolor = tc, buttonFunction = f}
	return #content.page1.pageContent.buttons-1
   end,
   drawButton = function(index)
	b = content.page1.pageContent.buttons[index]
	drawing.fillArea(b.color,b.x,b.y,b.width,b.height)	
	drawing.writeTextCentered(b.name,b.x,b.y,b.width,b.height,b.textcolor,b.color)
   end,
   getLabels = function()
	return content.page1.pageContent.labels
   end,
   addLabel = function(text,xpos,ypos,c,tc)
	content.page1.pageContent.labels[#content.page1.pageContent.labels+1] =
		 {name = text,x = xpos,y = ypos,labelLength = l,centred = cent,color = c,textcolor = tc}
   end,
   setLabelText = function(index,text)
	content.page1.pageContent.buttons[index].name = text
   end,
   drawLabel = function(index)
   --print("page 1 ",#content.page1.pageContent.labels)
   sleep(1)
	b = content.page1.pageContent.labels[index]
	drawing.fillArea(b.color,b.x,b.y,b.labelLength,1)
	if(b.centred)then
	 drawing.writeTextCentered(b.name,b.x,b.y,b.labelLength,1,b.textcolor,b.color)
	else
	 draw.writeText(b.name,b.x,b.y,b.textcolor,b.color)
	end
   end,
   addImage = function(i,xpos,ypos)
	images[#content.page1.pageContent.images] = {image = i,x = xpos,y = ypos}
   end,
   getImages = function()
	return images
   end
  }

},
page2 =
{
  background = nil,
  visible = false,
  setBackground = function(path)
   background = paintutils.loadImage(path)
  end,
  getBackground = function()
   return background
  end,
  getPageContent = function()
   return pageContent
  end,
  setVisable = function(vis)
   visible = vis
  end,
  pageContent =
  {
   buttons = {},
   labels = {},
   images = {},
   getButtons = function()
	return content.page2.pageContent.buttons
   end,
   addButton = function(text,xpos,ypos,wid,high,c,tc,f)
	content.page2.pageContent.buttons[#content.page2.pageContent.buttons+1] =
		   {name = text,x = xpos,y = ypos
		   ,width = wid,height = high,color = c
		   ,textcolor = tc, buttonFunction = f}
	return #content.page2.pageContent.buttons-1
   end,
   drawButton = function(index)
	b = content.page2.pageContent.buttons[index]
	drawing.fillArea(b.color,b.x,b.y,b.width,b.height)	
	drawing.writeTextCentered(b.name,b.x,b.y,b.width,b.height,b.textcolor,b.color)
   end,
   getLabels = function()
	return content.page2.pageContent.labels
   end,
   addLabel = function(text,xpos,ypos,c,tc)
	content.page1.pageContent.labels[#content.page1.pageContent.labels+1] =
		 {name = text,x = xpos,y = ypos,labelLength = l,centred = cent,color = c,textcolor = tc}
   end,
   setLabelText = function(index,text)
	content.page2.pageContent.buttons[index].name = text
   end,
   drawLabel = function(index)
   --print("page 2 ",#content.page2.pageContent.labels)
   sleep(1)
	b = content.page1.pageContent.labels[index]
	drawing.fillArea(b.color,b.x,b.y,b.labelLength,1)
	if(b.centred)then
	 drawing.writeTextCentered(b.name,b.x,b.y,b.labelLength,1,b.textcolor,b.color)
	else
	 draw.writeText(b.name,b.x,b.y,b.textcolor,b.color)
	end
   end,
   addImage = function(i,xpos,ypos)
	images[#content.page2.pageContent.images] = {image = i,x = xpos,y = ypos}
   end,
   getImages = function()
	return images
   end
  }
},
pages =
{
  content.page1,
  content.page2
}
}
drawing =
{
drawImage = function(image,x,y)
  paintutils.drawImage(image,x,y)
end,
fillArea = function(color,xpos,ypos,wid,high)
  term.setCursorPos(1,1)
  --print("<",currentPage.visible,">",xpos,",",ypos,",",wid)
  --sleep(1)
  term.setBackgroundColor(color)
  for y=ypos,ypos+high-1,1 do
   for x=xpos,xpos+wid-1,1 do
	term.setCursorPos(x,y)
	write(" ")
   end
  end
end,
writeText = function(text,x,y,color,bgcolor)
  term.setBackgroundColor(color)
  term.setCursorPos(x,y)
  write(text)
end,
writeTextCentered = function(text,x,y,wid,high,color,bgcolor)
  term.setCursorPos(x+(wid-#text)/2,y+high/2)
  term.setTextColor(color)
  term.setBackgroundColor(bgcolor)
  term.write(text)
end,
clear = function()
  term.setBackgroundColor(colors.black)
  term.clear()
  term.setCursorPos(1,1)
end
}
function drawPageContent()
showButtons(currentPage.pageContent.getButtons())
showLabels(currentPage.pageContent.getLabels())
end
function showButtons(button)
for x=1,#button,1 do
  currentPage.pageContent.drawButton(x)
end
end
function showLabels(label)
for x=1,#label,1 do
  currentPage.pageContent.drawLabel(x)
end
end
function analyseClick(xpos,ypos)
buttons = currentPage.pageContent.getButtons()
for i=1,#buttons,1 do
  if(xpos>=buttons[i].x and xpos<=buttons[i].x+buttons[i].width) then
   if(ypos>=buttons[i].y and ypos<=buttons[i].y+buttons[i].height) then
	buttons[i].buttonFunction()
   end
  end
end
end
running = true
currentPage = content.pages[1]
local termWidth,termHeight = term.getSize()
content.page1.setBackground("page1")
content.page2.setBackground("page2")
content.page1.pageContent.addButton("test",15,15,6,3,colors.blue,colors.black,function()end)
content.page1.pageContent.addButton("Exit",termWidth-5,1,6,3,colors.red,colors.white,function() running = false end)
content.page1.pageContent.addButton("test2",30,5,7,3,colors.green,colors.black,function()end)
content.page1.pageContent.addButton("Next Page",termWidth-10,termHeight-2,11,3,colors.orange,colors.white,function() currentPage = content.pages[2] end)
content.page2.pageContent.addButton("Next Page",termWidth-10,termHeight-2,11,3,colors.orange,colors.white,function() currentPage = content.pages[1] end)
content.page2.pageContent.addLabel("0",5,5,4,true,colors.white,colors.black)
content.page2.pageContent.addButton("/\\",5,4,4,1,colors.gray,colors.black,function() content.page2.pageContent.labels[1].setLabelText(tostring(tonumber(currentPage.pageContent.buttons[2].name)+1))end)
content.page2.pageContent.addButton("\\/",5,6,4,1,colors.gray,colors.black,function() content.page2.pageContent.labels[1].setLabelText(tostring(tonumber(currentPage.pageContent.buttons[2].name)-1))end)

while running do
drawing.clear()
drawing.drawImage(currentPage.getBackground(),1,1)
drawPageContent()
event, button, x, y = os.pullEvent("mouse_click")
analyseClick(x,y)
end
drawing.clear()
Edited on 01 June 2014 - 06:18 PM
GamerNebulae #2
Posted 01 June 2014 - 08:23 PM
Could you please post it on pastebin? As the editor here does not display line numbers.
roosterhat #3
Posted 01 June 2014 - 09:34 PM
http://pastebin.com/tSJnGdt5 <<old
http://pastebin.com/4NFpeEHA <<current
Edited on 01 June 2014 - 10:22 PM
TheOddByte #4
Posted 01 June 2014 - 10:10 PM
Please post correct errors.. :P/>

<program name>:136:attempt to index: a nil value
<program name>:152:attempt to perform arithemic add on number and nil


The error that appears on line 136 can be fixed by removing the table 'pages' from that table, then declare it outside the table like this

content.pages = {
	content.page1;
	content.page2;
}
Will try to see if I can fix the other error too ;)/>

Edit: It seems that it's an error on line 53

fillArea = function(color,xpos,ypos,wid,high)
    term.setCursorPos(1,1)
    --print("<",currentPage.visible,">",xpos,",",ypos,",",wid)
    --sleep(1)
    if type( xpos ) ~= "number" then error( "xpos: expected number, got " .. type( xpos ), 2 ) end
    if type( ypos ) ~= "number" then error( "ypos: expected number, got " .. type( ypos ), 2 ) end
    if type( wid ) ~= "number" then error( "wid: expected number, got " .. type( wid ), 2 ) end
    if type( high ) ~= "number" then error( "high: expected number, got " .. type( high ), 2 ) end
    term.setBackgroundColor(color)
    for y=ypos,ypos+high-1,1 do
        for x=xpos,xpos+wid-1,1 do -- It errors here since the variable wid is nil
            term.setCursorPos(x,y)
            write(" ")
        end
    end
end
This is line 53

drawing.fillArea(b.color,b.x,b.y,b.labelLength,1)
And it seems like the variable b.labelLength is nil, It has been declared to a variable called 'l' which is undeclared, causing it to be nil all the time, thus it errors :P/>
Edited on 01 June 2014 - 08:20 PM
roosterhat #5
Posted 01 June 2014 - 10:25 PM
i know i found that will looking for the problem, i found that when it called showLabels it some how found a label in page1's page content and tried to print it
i also found that if i changed the label location from page2 to page1 that it worked fine
TheOddByte #6
Posted 01 June 2014 - 11:10 PM
I was looking at this function

addLabel = function(text,xpos,ypos,c,tc)
    content.page1.pageContent.labels[#content.page1.pageContent.labels+1] = {name = text,x = xpos,y = ypos,labelLength = l,centred = cent,color = c,textcolor = tc}
end
And I've searched through the entire code and couldn't find that you're declaring the variable 'l' anywhere, it's just a nil variable
roosterhat #7
Posted 01 June 2014 - 11:29 PM
maybe that its i must have undid some code and removed that variable from the function paramerteres
roosterhat #8
Posted 01 June 2014 - 11:35 PM
ya i must have because i dont have the parameter for centered either
ill add those parameters and see what it does
roosterhat #9
Posted 02 June 2014 - 12:23 AM
so the label problem is fixed i just need to figure out why it prints the same background for both pages
roosterhat #10
Posted 02 June 2014 - 07:10 AM
update:
i fixed the background problem
TheOddByte #11
Posted 02 June 2014 - 02:36 PM
update:
i fixed the background problem
Oh that's good, Also.. There is an edit button ;)/> :P/>