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

Help with DW20 button API

Started by kpfromer, 08 November 2013 - 05:23 PM
kpfromer #1
Posted 08 November 2013 - 06:23 PM
I need HELP! It says attempt to index! What do I do?


Program button:

local mon = peripheral.wrap("top")
mon.setTextScale(1)
mon.setTextColor(colors.white)
local button={}
mon.setBackgroundColor(colors.black)

function clearTable()
button = {}
mon.clear()
end

function setTable(name, func, xmin, xmax, ymin, ymax)
button[name] = {}
button[name]["func"] = func
button[name]["active"] = false
button[name]["xmin"] = xmin
button[name]["ymin"] = ymin
button[name]["xmax"] = xmax
button[name]["ymax"] = ymax
end

function funcName()
print("You clicked buttonText")
end

function fillTable()
setTable("ButtonText", funcName, 5, 25, 4, 8)
end

function fill(text, color, bData)
mon.setBackgroundColor(color)
local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
for j = bData["ymin"], bData["ymax"] do
mon.setCursorPos(bData["xmin"], j)
if j == yspot then
for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
if k == xspot then
mon.write(text)
else
mon.write(" ")
end
end
else
for i = bData["xmin"], bData["xmax"] do
mon.write(" ")
end
end
end
mon.setBackgroundColor(colors.black)
end

function screen()
local currColor
for name,data in pairs(button) do
local on = data["active"]
if on == true then currColor = colors.lime else currColor = colors.red end
fill(name, currColor, data)
end
end

function toggleButton(name)
button[name]["active"] = not button[name]["active"]
screen()
end

function flash(name)
toggleButton(name)
screen()
sleep(0.15)
toggleButton(name)
screen()
end

function checkxy(x, y)
for name, data in pairs(button) do
if y>=data["ymin"] and y <= data["ymax"] then
if x>=data["xmin"] and x<= data["xmax"] then
data["func"]()
return true
–data["active"] = not data["active"]
–print(name)
end
end
end
return false
end

function heading(text)
w, h = mon.getSize()
mon.setCursorPos((w-string.len(text))/2+1, 1)
mon.write(text)
end

function label(w, h, text)
mon.setCursorPos(w, h)
mon.write(text)
end




Program con:



os.loadAPI("button")
m = peripheral.wrap("left")
m.clear()

function fillTable()
button.setTable("Front", front, 5,10,3,4)
button.screen()
end

function getClick()
event,side,x,y = os.pullEvent("monitor_touch")
button.checkxy(x,y)
end

function front()
button.toggleButton("Front")
rs.setBundledOutput("back", colors.white)
sleep(2)
rs.setBundledOutput("back", 0)
end

fillTable()
button.heading("Control Center")
button.label(0,0,"")
while true do
getClick()
end
Lyqyd #2
Posted 08 November 2013 - 06:43 PM
Split into new topic.

I'm not really sure why you originally posted this in my Touchpoint API topic, since you're using the DW20 button API.
kpfromer #3
Posted 08 November 2013 - 06:52 PM
Sorry, but how can i fix it?
Lyqyd #4
Posted 08 November 2013 - 07:55 PM
What's the full error text? There should be a line number and it should tell you what the actual value was.
kpfromer #5
Posted 08 November 2013 - 08:25 PM
What do you mean? All it says is "button:2: attempt to index ? (a nil value)" This happens when I run both programs!
Kingdaro #6
Posted 08 November 2013 - 08:30 PM
There's no monitor on top of your computer.
kpfromer #7
Posted 08 November 2013 - 08:49 PM
Thanks i fixed that but now it says "con:2: attempt to call nil"!
kpfromer #8
Posted 08 November 2013 - 09:35 PM
Fixed it thanks!