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

Buttons

Started by grand_mind1, 07 May 2013 - 08:38 PM
grand_mind1 #1
Posted 07 May 2013 - 10:38 PM
I am working on a button API. Currently, there is a function that I use the fill in a table with the coordinates of the button, the name, and the text that's on it and another function that creates a box using the coordinates from the table and the color as a parameter.
Code:

local button = {}
function fillTable(name, xmix, xmax, ymin, ymax, text)
button[name] = {}
button[name]["text"] = text
button[name]["xmix"] = xmin
button[name]["xmax"] = xmax
button[name]["ymin"] = ymin
button[name]["ymax"] = ymax
end
function createBox(name, color)
term.setBackgroundColor(color)
currentY = button[name]["ymin"]
while currentY ~= button[name]["ymax"] do
  term.setCursorPos(button[name]["xmin"], currentY)
  for i = 1,button[name]["xmax"] do
   write(" ")
		end
		currentY = currentY +1
	end
end

It is the function that creates the box that is giving me trouble. I keep getting this error:

btn:17: Expected number, number
When I look at line 17 I can't find anything wrong with it. Both of the coordinates should be numbers. If I change them from tables to just straight up numbers it works fine. It seems strange because everywhere else it works fine.
Help is appreciated!
Thanks! :D/>

P.S. Sorry for some of the weird(or lack of) indentation. I just started using sublime and when I copy it from there to here the indentation gets messed up sometimes.
HurricaneCoder #2
Posted 07 May 2013 - 11:05 PM
I am working on a button API. Currently, there is a function that I use the fill in a table with the coordinates of the button, the name, and the text that's on it and another function that creates a box using the coordinates from the table and the color as a parameter.
Code:

local button = {}
function fillTable(name, xmix, xmax, ymin, ymax, text)
button[name] = {}
button[name]["text"] = text
button[name]["xmix"] = xmin
button[name]["xmax"] = xmax
button[name]["ymin"] = ymin
button[name]["ymax"] = ymax
end
function createBox(name, color)
term.setBackgroundColor(color)
currentY = button[name]["ymin"]
while currentY ~= button[name]["ymax"] do
  term.setCursorPos(button[name]["xmin"], currentY)
  for i = 1,button[name]["xmax"] do
   write(" ")
		end
		currentY = currentY +1
	end
end

It is the function that creates the box that is giving me trouble. I keep getting this error:

btn:17: Expected number, number
When I look at line 17 I can't find anything wrong with it. Both of the coordinates should be numbers. If I change them from tables to just straight up numbers it works fine. It seems strange because everywhere else it works fine.
Help is appreciated!
Thanks! :D/>

P.S. Sorry for some of the weird(or lack of) indentation. I just started using sublime and when I copy it from there to here the indentation gets messed up sometimes.


You can try to change your createBox() to this code and make sure you define your table.
function createBox()
term.setBackgroundColor(What Ever Colors You Want)
for k,v in pairs(button) do
for i = v["ymin"],v["ymax"] do
term.setCursorPos(v["xmin"],i)
term.write(" ")
for g = v["xmin"],v["xmax"] do
term.setCursorPos(g,i)
term.write(" ")
end
end
end
end


Tell me if there is something wrong with my code I'll change it.
Hope This Help =)
grand_mind1 #3
Posted 07 May 2013 - 11:14 PM
Thanks but I kind of wanted to just figure out what's wrong with my code not change it to something else. (If possible) I know that your code will work and I know that it is much more clean than mine but I don't understand it. Even if I did understand it I don't think I would get that same sense of pride or accomplishment when I finished the whole API if I knew that I had just copied it or didn't understand some of it.
HurricaneCoder #4
Posted 07 May 2013 - 11:39 PM
Thanks but I kind of wanted to just figure out what's wrong with my code not change it to something else. (If possible) I know that your code will work and I know that it is much more clean than mine but I don't understand it. Even if I did understand it I don't think I would get that same sense of pride or accomplishment when I finished the whole API if I knew that I had just copied it or didn't understand some of it.


So there is currently one thing wrong with your code. you spell xmin wrong. It should be "xmin" but you spell "xmix".
So here is your code that I correct for you and test for you it work fine. I am not so sure what error is about but I am almost positive that it has something to do with you spelling xmin wrong.



local button = {}
function fillTable(name, xmin, xmax, ymin, ymax, text)
button[name] = {}
button[name]["text"] = text
button[name]["xmin"] = xmin
button[name]["xmax"] = xmax
button[name]["ymin"] = ymin
button[name]["ymax"] = ymax
end

fillTable("test",10,19,5,10,"text1")
fillTable("test2",10,19,13,18,"text2")

function createBox(name, color)
term.setBackgroundColor(colors.blue)
currentY = button[name]["ymin"]
while currentY ~= button[name]["ymax"] do
  term.setCursorPos(button[name]["xmin"], currentY)
  for i = button[name]["xmin"],button[name]["xmax"] do
   write(" ")
				end
				currentY = currentY +1
		end
end
createBox("test")
createBox("test2")


Tell Me If You Have Anymore Question
nitrogenfingers #5
Posted 07 May 2013 - 11:47 PM
Took me awhile to spot, but it's a spelling error:

function fillTable(name, xmix, xmax, ymin, ymax, text)
button[name] = {}
button[name]["text"] = text
button[name]["xmix"] = xmin

Parameter 2 should be xmin, rather than xmix. Ditto for the table entry. Replace with:


function fillTable(name, xmin, xmax, ymin, ymax, text)
button[name] = {}
button[name]["text"] = text
button[name]["xmin"] = xmin

Otherwise your for loop is referencing xmin, which is just null.
grand_mind1 #6
Posted 08 May 2013 - 07:43 AM
Ah! I don't know why but I was doing that a lot when writing this. I would accidentally press x instead of n as if I was typing max. Thanks!