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

[Question] Button API + Nuclear Information Reader = Errors for Dayz?

Started by GreatOak, 01 May 2013 - 11:25 PM
GreatOak #1
Posted 02 May 2013 - 01:25 AM
Title: [Question] Button API + Nuclear Information Reader = Errors for Dayz? :(/>

I've gone over this script for ages honestly, but each time I make a 'breakthrough' it breaks again :D/>. Great….Well please can you tell me where my button api might've gone south :)/>. I think I have each class lined up in the right scope, but some functions keep receiving a nil statement.

Here are the scripts:
Nuclear Information Reader/Monitor Display
Spoiler

--Beginning Declarations
monitor =  peripheral.wrap("top")
monitor.clear()
nir = peripheral.wrap("left")
local nuke = paintutils.loadImage("nuke")
os.loadAPI("disk/buttons")
addButton("NuclearReactorToggle",no,reactorToggle,colors.yellow,colors.yellow,colors.black,1,1,25,1)
function reactorToggle()
  rednet.open("bottom")
  if reactorNumb == 1 then
	rednet.send(255,on)
  elseif reactorNumb == 2 then
	rednet.send(254,on)
  elseif reactorNumb == 3 then
	rednet.send(254,on)
  elseif reactorNumb == 4 then
	rednet.send(253,on)
  end
end
--System Functions
function heat()
for system,status in pairs(info) do
  status = tostring(status)
  max = 500
  if system == "heat" then
	monitor.setCursorPos(1,3)
	monitor.setTextColor(colors.yellow)
	print("*")
	monitor.setCursorPos(1,5)
	print("*")
	monitor.setTextColor(colors.white)
	monitor.setCursorPos(2,3)
	print(" Reactor Heat Level = "..status)
	monitor.setCursorPos(2,5)
	print(" Reactor Max Heat = "..max)
	monitor.setTextColor(colors.yellow)
	monitor.setCursorPos(27,5)
	print("+")
	monitor.setTextColor(colors.white)
	monitor.setCursorPos(28,5)
	print(" or ")
	monitor.setCursorPos(33,5)
	monitor.setTextColor(colors.yellow)
	print("-")
  end
end
end
function reactorPower()
for system,status in pairs(info) do
  status = tostring(status)  
  if system == "reactorPoweredB" then
	monitor.setBackgroundColor(colors.yellow)	  
	monitor.setTextColor(colors.black)
	monitor.setCursorPos(1,1)
	for name,data in pairs(disk/buttons) do
	  local on = data["active"]
	  if on == true then
		reactorStatus = "on"
	  else
		reactorStatus = "off"
	  end
	end
	print("Nuclear Reactor #"..reactorNumb.." = "..reactorStatus)
	monitor.setCursorPos(1,18)
	print("Back")
	monitor.setCursorPos(28,18)
	print("Page Forward")
	monitor.setBackgroundColor(colors.black)
  end
end
end
function output()
for system,status in pairs(info) do
  status = tostring(status)
  if system == "output" then
	monitor.setTextColor(colors.yellow)
	monitor.setCursorPos(1,7)
	print("*")
	monitor.setTextColor(colors.white)
	monitor.setCursorPos(2,7)
	print(" EU Output = "..status)
  end
end
end
function timeLeft()
for system,status in pairs(info) do
  status = tostring(status)
  if system == "timeLeft" then
	monitor.setTextColor(colors.yellow)
	monitor.setCursorPos(1,9)
	print("*")
	monitor.setTextColor(colors.white)
	monitor.setCursorPos(2,9)
	print(" Uranium Cells level = "..status)
  end
end
end
function nuclear(numbNuke)
  noth1, noth2, card, info = nir.get(numbNuke)
  reactorNumb = numbNuke
  term.redirect(monitor)
  monitor.clear()
  monitor.setBackgroundColor(colors.black)
  heat()
  reactorPower()
  output()
  timeLeft()
  term.restore()
end
--Load Screen Function
local function loadScreen()
  monitor.clear()
  term.redirect(monitor)
  monitor.setTextColor(colors.white)
  monitor.setBackgroundColor(colors.black)
  monitor.setCursorPos(9,1)
  textutils.slowWrite("Nuclear Reactor Controls")
  paintutils.drawImage(nuke,-4,2)
  monitor.setCursorPos(1,15)
  textutils.slowWrite("(Right Click Screen to go edit Reactor)")
  while os.pullEvent("monitor_touch") == false do
	sleep(0)
  end
  monitor.clear()
  term.restore()
end
--Load Screen Function
loadScreen()
--Nuclear #1 Display/Controls
nuclear(1)
while true do
  nuclear(1)
  buttonLoop()
  sleep(0)
end
local event,side,xPos,yPos = os.pullEvent("monitor_touch")
Button API (Based off my boy Direwolf's Button API, but modified #Plagiarism)
Spoiler
buttons = {} --Button Array of Sorts
function addButton(name,displaytext,func,coloron,coloroff,textcolor,xmin,xmax,ymin,ymax)
  buttons[name] = {}
  buttons[name]["displaytext"] = displaytext
  buttons[name]["func"] = func
  buttons[name]["xmin"] = xmin
  buttons[name]["xmax"] = xmax
  buttons[name]["ymin"] = ymin
  buttons[name]["ymax"] = ymax
  buttons[name]["coloron"] = coloron
  buttons[name]["coloroff"] = coloroff
  buttons[name]["textcolor"] = textcolor
  buttons[name]["active"] = false
end
function listButtons()
  for name,data in pairs(buttons) do
	name = tostring(name)
	print(name)
  end
end
function fillButton(text,bData,color)
  term.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
	term.setCursorPos(bData["xmin"],j)
	if j == yspot then
	  for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
		if k == xspot then
		  term.setTextColor(bData["textcolor"])
		  if bData["displaytext"] == yes then
			term.write(text)
		  elseif bData["displaytext"] == no then
			term.write(" ")
		  end
		else
		  term.write(" ")
		end
	  end
	else
	  for i = bData["xmin"], bData["xmax"] do
		term.write(" ")
	  end
	end
  end
  term.setBackgroundColor(colors.black)
end
function displayButton()
  local currColor
  for name,data in pairs(buttons) do
	local on = data["active"]
	if on == true then currColor = data["coloron"] else currColor = data["coloroff"] end
	fillButton(name,data,currColor)
  end
end
function xyCheck(x,y)
  for name,data in pairs(button) do
	if y>=data["ymin"] and y<=data["ymax"] then
	  data["func"]()
	  data["active"] = not data["active"]
	  print(name)
	end
  end
end
function buttonLoop()
  displayButton()
  local e,side,x,y = os.pullEvent("monitor_touch")
  xyCheck(x,y)
  sleep(.1)
end
buttons.listButtons()
Honestly I've spent way too much time on this, and it is time for me to get help. So a response would be definitely helpful(if you know what your doing….)

-GreatOak

http://pastebin.com/8zQxcK4F –PasteBin for Startup Script:

http://pastebin.com/aByRVkHY –PasteBin for Button API:

Update: I had buttons.listButtons() on the buttonAPI, but it still doesn't working calling nil on the line where I declare the button on startup script. Next time I'll use a button api someone else wrote instead of programming my own interpretation lol.
Edited on 08 May 2013 - 04:09 PM
Metalhead #2
Posted 30 October 2014 - 09:11 AM
you have to call buttons.addButton(), not addButton()

you must write API name before calling functions from it

Thanks for ButtonAPI, i've fixed bugs in it