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

Plz help the code won't work.

Started by Brandon_sanc, 17 September 2014 - 11:26 PM
Brandon_sanc #1
Posted 18 September 2014 - 01:26 AM
os.loadAPI("button")
rednet.open("bottom")
local mon = peripheral.wrap("top")

function fillTable()
button.setTable("Mall", mall, 3,16,3,5)
button.setTable("Mall", mall, 3,16,7,9)
button.screen()
end

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

function mall()
print("mall clicked")
rednet.send(5, "mall")
button.flash("Mall")
end

function n/a()
print("n/a clicked")
rednet.send(5, "n/a")
button.flash("N/A")
end

fillTable()
button.heading("Station")

while true do
getEvent()
end

Its states "bios:338: [string"trainScreen"]:24: '(' expected

I don't understand why or how to fix this please help. Thanks in advance
Dog #2
Posted 18 September 2014 - 01:36 AM
You can't have a slash in the name of a variable/function. Change…

function n/a
to…

function NA
or something similar (without the slash) - and make sure your calls to the function are changed as well.
Brandon_sanc #3
Posted 18 September 2014 - 01:53 AM
K

Let me try it

Now it says bios:339: [strinf"button"]:77: 'then' expected trainScreen:3: attempt to call nil

I will give you the code for the button now.

local mon = peripheral.wrap("top")
print("button loaded")
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
Dog #4
Posted 18 September 2014 - 02:09 AM
Please place your code between code tags when posting code - and also, please indent your code so it's easier to read and troubleshoot.

[.CODE]
–# code here
[./CODE]

(but without the preceding dots)

Like so…

local mon = peripheral.wrap("top")
print("button loaded")
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

Based on the code you posted, I can't see where the error is happening (there are no if/then statements near line 77). Since we're dealing with a fair bit of code, please post your code to Pastebin and provide links so I can review and test the entire code myself.