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

I dont know how to fix this Bios 338 error

Started by Termanater13, 09 July 2013 - 11:33 PM
Termanater13 #1
Posted 10 July 2013 - 01:33 AM
Im trying to right an API to add buttons to a monitor similar to Direwolf20's button API and as I get close to getting it done I get a "bios:338: [string "button"]:2: unexpected symbol" error and on line 2 of my code looks fine to me and I have no idea whats wrong. it worked untill I added a setting saving part to the variable.

its in spoiler tags due to its length and I have no Idea what part could be causing it.
Spoiler
local Buttonapi = {}
local Buttonapi["data"] = {}
local Buttonapi["setting"] = {}
--this function is called to setup the
--button api for the current  instance
--************************************
--need a way to save setup data
function setup(option,setting)
if option == "wrap" then
  mon = peripheral.wrap(setting)
  Buttonapi["setting"]["wrap"] = setting
elseif option == "textscale" then
  mon.setTextScale(setting)
  Buttonapi["setting"]["textscale"] = setting
elseif option == "textcolor" then
  mon.setTextColor(setting)
  Buttonapi["setting"]["textcolor"] = setting
elseif option == "backgroundcolor" then
  mon.setBackgroundColor(setting)
  Buttonapi["setting"]["backgroundcolor"] = setting
elseif option == "basic" then
  if setting == nil then
   mon = peripheral.wrap("top")
   Buttonapi["setting"]["wrap"] = "top"
  else
   mon = peripheral.wrap(setting)
   Buttonapi["setting"]["wrap"] = setting
  end
  mon.setTextScale(1)
  mon.setTextColor(colors.white)
  mon.setBackgroundColor(colors.black)
  Buttonapi["setting"]["textscale"] = 1
  Buttonapi["setting"]["textcolor"] = colors.white
  Buttonapi["setting"]["backgroundcolor"] = colors.black
else
  print("Option not reconized. Please use (option, setting) for function. possible options are wrap, textscale, textcolor, backgroundcolor, basic.")
end
end
--adds a color checking function for
--easyer coding with api, you supply a
--name or number for the color rather
--than using CC's defult color api.
--This makes it more new user freindly.
function colorchk(color, defultcolor)
if type(color) == "number" then
  for i = 0, 15, 1 do
   if (2^i) == color then
	return color
   else
	return defultcolor
   end
  end
elseif type(color) == "string" then
  if color == "white" then
   return colors.white
  elseif color == "orange" then
   return colors.orange
  elseif color == "magenta" then
   return colors.magenta
  elseif color == "lightblue" then
   return colors.lightBlue
  elseif color == "yellow" then
   return colors.yellow
  elseif color == "lime" then
   return colors.lime
  elseif color == "pink" then
   return colors.pink
  elseif color == "gray" then
   return colors.gray
  elseif color == "lightgray" then
   return colors.lightGray
  elseif color == "cyan" then
   return colors.cyan
  elseif color == "purple" then
   return colors.purple
  elseif color == "blue" then
   return colors.blue
  elseif color == "brown" then
   return colors.brown
  elseif color == "green" then
   return colors.green
  elseif color == "red" then
   return colors.red
  elseif color == "black" then
   return colors.black
  end
else
  return defultcolor
end
end
--sets the api call that creats a new
--button in the table.
function new(name, usefunc, xmin, xmax, ymin, ymax, neColor, enColor)
Buttonapi["data"][name]={}
Buttonapi["data"][name]["usefunc"] = usefunc
Buttonapi["data"][name]["xmin"] = xmin
Buttonapi["data"][name]["xmax"] = xmax
Buttonapi["data"][name]["ymin"] = ymin
Buttonapi["data"][name]["ymax"] = ymax
Buttonapi["data"][name]["active"] = false
Buttonapi["data"][name]["disabled"] = colorchk(neColor, colors.red)
Buttonapi["data"][name]["enabled"] = colorchk(enColor, colors.lime)
end
--toggles atctive statuse of the button
function toggle(name)
Buttonapi["data"][name]["active"] = not Buttonapi["data"][name]["active"]
display(name)
end
--finds where to place text based on x
local function findx(xl,xh,tl)
ts=xh-xl-tl
if (ts % 2) == 0 then
  return (ts/2)
else
  return (math.floor(ts/2)+1)
end
end
--finds where to place text based on y
local function findy(yl,yh)
return (math.floor((yl+yh)/2))
end
--displayes th buttons created
--needs to be looked over throws errors
function display()
for key, value in pairs(Buttonapi["data"]) do
  if value["active"] then
   mon.setBackgroundColor(value["enabled"])
  else
   mon.setBackgroundColor(value["disabled"])
  end
  xspot = findx(value["xmin"], value["xmax"], string.len(key))
  yspot = findy(value["ymin"], value["ymax"])
  for y = value["ymin"], value["ymax"] do
   mon.setCursorPos(value["xmin"],y)
   if y == yspot then
	for x = 0, (value["xmax"]-value["xmin"]-string.len(key)+1) do
	 if x == xspot then
	  mon.write(key)
	 else
	  mon.write(" ")
	 end
	end
   else
	for x = 0, (value["xmax"]-value["xmin"]) do
		mon.write(" ")
	end
   end
  end
  --reset background color here
end
end
--checks the x and y of place clicked
--to call the right function
function check(x,y)
for key, value in pairs(Buttonapi["data"]) do
  if y>=value["ymin"] and y<=value["ymax"] then
   if x>=value["xmin"] and x<=value["xmax"] then
	value["usefunc"]()
	return true
   end
  end
end
return false
end
function heading(text)
w, h = mon.getSize()
l = string.len(text)
mon.setCursorPos(math.ceil((w-l)/2)+1,1)
mon.write(text)
end
function text(x, y, text)
mon.setCursorPos(x,y)
mon.write(text)
end
Engineer #2
Posted 10 July 2013 - 03:47 AM
After a bit of searching, the solution is very simple. Here are the top 3 lines:

local Buttonapi = {}
local Buttonapi["data"] = {}
local Buttonapi["setting"] = {}

Since the table Buttonapi is already local, you cannot make keys local too. So this is the fix:

local Buttonapi = {}
Buttonapi["data"] = {}
Buttonapi["setting"] = {}
Termanater13 #3
Posted 10 July 2013 - 10:31 AM
That fixed it. I will keep it in mind next time something like that would happen
GopherAtl #4
Posted 10 July 2013 - 11:13 AM
in general, not only can you not declare an index in a table local like you were doing, you should take care to only declare a given variable as local once in your program. Each time you declare it local, it creates a new local var with that name which is only used from that point on. To demonstrate…


local foo=1

function f1()
  print("f1 : "..foo)
end

local foo=2
function f2()
  print("f2 : "..foo)
end

f1()
f2()
You might expect f1 and f2 to both display 2, but they don't. f1 will print "f1 : 1" and f2 will print "f2 : 2". Any code before the second "local foo" line will continue to access the original foo variable, any code after will access the second foo variable.
Engineer #5
Posted 10 July 2013 - 12:47 PM
in general, not only can you not declare an index in a table local like you were doing, you should take care to only declare a given variable as local once in your program. Each time you declare it local, it creates a new local var with that name which is only used from that point on. To demonstrate…


local foo=1

function f1()
  print("f1 : "..foo)
end

local foo=2
function f2()
  print("f2 : "..foo)
end

f1()
f2()
You might expect f1 and f2 to both display 2, but they don't. f1 will print "f1 : 1" and f2 will print "f2 : 2". Any code before the second "local foo" line will continue to access the original foo variable, any code after will access the second foo variable.
Its pretty much the same as:

local foo = 1

...

foo = 2
But I see your point though


Woa, big brainfart, sorry!
GopherAtl #6
Posted 10 July 2013 - 03:42 PM
heh, n/p. It's very unexpected behavior by most standards!