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

Issue with a Function not running please help

Started by Tmq5521, 17 June 2014 - 03:55 PM
Tmq5521 #1
Posted 17 June 2014 - 05:55 PM
I have been attempting to create a BigReactor control monitor for my single player and my program is not running a function when called. the reactor setup is over the wired modem. the setup assumes a computer connected to a (5x3)monitor via wire and a reactor with five control rods named 1 - 5 connected via wire.
please suggest fixes.


--setup
local disp = peripheral.wrap("monitor_0")
local reac = peripheral.wrap("BigReactors-Reactor_0")
disp.clear()
disp.write("Reactor booting up")
disp.setCursorPos(1,1)
print(os.getComputerLabel().."has started.")
if reac.getActive() then reac.setActive(false) end
--Button and Nameplate Lists
power = {x = 2,y = 2,lx = 7,ly = 0,c = colors.yellow,n = " Power "}
rod1 = {x = 10,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 1 "}
rod2 = {x = 14,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 2 "}
rod3 = {x = 18,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 3 "}
rod4 = {x = 22,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 4 "}
rod5 = {x = 26,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 5 "}
rodAll = {x = 30,y = 2,lx = 5,ly = 0,c = colors.orange,n = " All "}
rod1Up = {x = 10,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod1Do = {x = 10,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rod2Up = {x = 14,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod2Do = {x = 14,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rod3Up = {x = 18,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod3Do = {x = 18,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rod4Up = {x = 22,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod4Do = {x = 22,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rod5Up = {x = 26,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod5Do = {x = 26,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rodAllUp = {x = 31,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rodAllDo = {x = 31,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
temp = {x = 36,y = 2,lx = 6,ly = 0,c = colors.red,n = " Temp "}
cool = {x = 44,y = 2,lx = 6,ly = 0,c = colors.blue,n = " Cool "}
names = {power,rod1,rod2,rod3,rod4,rod5,rodAll,temp,cool,rod1Up,rod1Do,rod2Up,rod2Do,rod3Up,rod3Do,rod4Up,rod4Do,rod5Up,rod5Do,rodAllUp,rodAllDo}
--Bars
bars = {bfuel,brod1,brod2,brod3,brod4,brod5,brodAll,btemp}
bfuel = {x1 = 2,y1 = 3,x2 = 7,y2 = 18,c = colors.yellow}
brod1 = {x = 10,y1 = 3,x2 = 13,y2 = 18,c = colors.grey}
brod2 = {x = 14,y1 = 3,x2 = 17,y2 = 18,c = colors.grey}
brod3 = {x = 18,y1 = 3,x2 = 21,y2 = 18,c = colors.grey}
brod4 = {x = 22,y1 = 3,x2 = 25,y2 = 18,c = colors.grey}
brod5 = {x = 26,y1 = 3,x2 = 29,y2 = 18,c = colors.grey}
brodAll = {x = 31,y1 = 3,x2 = 36,y2 = 18,c = colors.grey}
btemp = {x1 = 36,y1 = 2,x2 = 43,y2 = 18,c = colors.red}
function barUpdate()
  print('barUpdate')
  bfuel.dark,a = math.modf(((reactor.fuel/reac.getFuelAmountMax())/15)*100)
  brod1.dark,a = math.modf((reactor.rod1/14))
  brod2.dark,a = math.modf((reactor.rod2/14))
  brod3.dark,a = math.modf((reactor.rod3/14))
  brod4.dark,a = math.modf((reactor.rod4/14))
  brod5.dark,a = math.modf((reactor.rod5/14))
  brodAll.dark,a = math.modf((reactor.rodAll/14))
  btemp.dark,a = reactor.caseTemp/1
  print('')
end
function drawBar()
  term.redirect(disp)
  for I = 1,#bars do
    local b = bars[I]
    print('barDraw')
    print(B)/>/>
    print(b.x1)
    print(b.y1)
    print(b.x2)
    print(b.y2)
    print(b.c)
    print(b.dark)
    print('')
    paintutils.drawLine(b.x2,b.y2,b.x1,b.y1-b.dark,b.c)
  end
  term.redirect(term.native())
end
--[[function barDraw()
  for I = 1,#bars do
    local b = bars[I]
    disp.setBackgroundColor(b.c)
    --print entire bar
    for y = b.y1,b.y2 do
      disp.setCursorPos(b.x,y)
      disp.write(string.rep(" ",b.x2-b.x1))
    end
    --erase part
    disp.setBackGroundColor(colors.black)
    for y = b.y1,(b.y1+b.dark) do
      for x = b.x1,b.x2 do
        disp.setCursorPos(x,y)
        disp.write(" ")
      end
    end
  end
end]]--
reactor = {}
function updateReactor()
  --List containing relevant info
  reactor.active = reac.getActive()
  reactor.caseTemp = reac.getCasingTemperature()
  reactor.rod1 = reac.getControlRodLevel(0)
  reactor.rod2 = reac.getControlRodLevel(1)
  reactor.rod3 = reac.getControlRodLevel(2)
  reactor.rod4 = reac.getControlRodLevel(3)
  reactor.rod5 = reac.getControlRodLevel(4)
  print('updateReactor')
  print(reactor.rod1)
  print(reactor.rod2)
  print(reactor.rod3)
  print(reactor.rod4)
  print(reactor.rod5)
  print(reactor.caseTemp)
  print(reactor.rodAll)
  print('')
  reactor.rodAll = (reactor.rod1+reactor.rod2+reactor.rod3+reactor.rod4+reactor.rod5)/5
  reactor.cool = reac.getCoolantAmount()
  reactor.fuel = reac.getFuelAmount()
end
function drawBox()
  for i = 1,#names do
    b = names[i]
    disp.setBackgroundColor(b.c)
    for T = 0,b.ly do disp.setCursorPos(b.x,b.y+T)
      for E = 1,b.lx do
        disp.write(" ")
      end
    end
    disp.setCursorPos(b.x,b.y)
    disp.setTextColor(colors.white)
    disp.write(b.n)
  end
  disp.setBackgroundColor(colors.black)
  disp.write(" ")
end
disp.clear()
drawBox()
while true do --My main loop (just running display currently)
  updateReactor()
  barUpdate()
  drawBar()  -- this is the function I have issues with
  sleep(2)
end
LBPHacker #2
Posted 17 June 2014 - 08:13 PM
Which is the function your program doesn't run (call)? (Actually, every function defined in your program gets called at least once).
Edited on 17 June 2014 - 06:14 PM
Tmq5521 #3
Posted 17 June 2014 - 08:25 PM
drawBar() never runs. I know this from the print statements never print.
wieselkatze #4
Posted 17 June 2014 - 08:39 PM
I think the problem is, that he defined bfuel, brod1 etc. after he inserted them in the actual table he wants to call.
Thus leading to the length of the table being 0 and the for loop never being executed, because all the variables are nil.

Try putting bars = {bfuel,brod1,brod2,brod3,brod4,brod5,brodAll,btemp} after btemp. That should work.
Edited on 17 June 2014 - 06:41 PM
Tmq5521 #5
Posted 17 June 2014 - 08:49 PM
Thanks for the help wieselkatze that fixed it.
wieselkatze #6
Posted 17 June 2014 - 08:57 PM
As for
drawBar() never runs. I know this from the print statements never print.

that is false.
You know that this loop never gets executed, but with the prints you still don't know if the function gets called.
Edited on 17 June 2014 - 06:57 PM
Tmq5521 #7
Posted 17 June 2014 - 09:13 PM
I now have a new problem: startup:77:expected number,number

Spoiler

--setup
local disp = peripheral.wrap("monitor_0")
local reac = peripheral.wrap("BigReactors-Reactor_0")
disp.clear()
disp.write("Reactor booting up")
disp.setCursorPos(1,1)
print(os.getComputerLabel().."has started.")
if reac.getActive() then reac.setActive(false) end
--Button and Nameplate Lists
power = {x = 2,y = 2,lx = 7,ly = 1,c = colors.yellow,n = " Power "}
rod1 = {x = 10,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 1 "}
rod2 = {x = 14,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 2 "}
rod3 = {x = 18,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 3 "}
rod4 = {x = 22,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 4 "}
rod5 = {x = 26,y = 2,lx = 3,ly = 0,c = colors.orange,n = " 5 "}
rodAll = {x = 30,y = 2,lx = 5,ly = 0,c = colors.orange,n = " All "}
rod1Up = {x = 10,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod1Do = {x = 10,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rod2Up = {x = 14,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod2Do = {x = 14,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rod3Up = {x = 18,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod3Do = {x = 18,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rod4Up = {x = 22,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod4Do = {x = 22,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rod5Up = {x = 26,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rod5Do = {x = 26,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
rodAllUp = {x = 31,y = 3,lx = 3,ly = 0,c = colors.orange,n = "^^^"}
rodAllDo = {x = 31,y = 18,lx = 3,ly = 0,c = colors.orange,n = "vvv"}
temp = {x = 36,y = 2,lx = 6,ly = 0,c = colors.red,n = " Temp "}
cool = {x = 44,y = 2,lx = 6,ly = 0,c = colors.blue,n = " Cool "}
names = {power,rod1,rod2,rod3,rod4,rod5,rodAll,temp,cool,rod1Up,rod1Do,rod2Up,rod2Do,rod3Up,rod3Do,rod4Up,rod4Do,rod5Up,rod5Do,rodAllUp,rodAllDo}
--Bars
bfuel = {x1 = 2,y1 = 4,x2 = 7,y2 = 18,c = colors.yellow}
brod1 = {x = 10,y1 = 3,x2 = 13,y2 = 18,c = colors.gray}
brod2 = {x = 14,y1 = 3,x2 = 17,y2 = 18,c = colors.gray}
brod3 = {x = 18,y1 = 3,x2 = 21,y2 = 18,c = colors.gray}
brod4 = {x = 22,y1 = 3,x2 = 25,y2 = 18,c = colors.gray}
brod5 = {x = 26,y1 = 3,x2 = 29,y2 = 18,c = colors.gray}
brodAll = {x = 31,y1 = 3,x2 = 36,y2 = 18,c = colors.gray}
btemp = {x1 = 36,y1 = 2,x2 = 43,y2 = 18,c = colors.red}
bars = {bfuel,brod1,brod2,brod3,brod4,brod5,brodAll,btemp}
function barUpdate()
  print('barUpdate')
  bfuel.dark,a = math.modf(((reactor.fuel/reac.getFuelAmountMax())/15)*100)
  brod1.dark,a = math.modf((reactor.rod1/14))
  brod2.dark,a = math.modf((reactor.rod2/14))
  brod3.dark,a = math.modf((reactor.rod3/14))
  brod4.dark,a = math.modf((reactor.rod4/14))
  brod5.dark,a = math.modf((reactor.rod5/14))
  brodAll.dark,a = math.modf((reactor.rodAll/14))
  btemp.dark,a = reactor.caseTemp/1
  print('')
end
--[[function drawBar()
  term.redirect(disp)
  for I = 1,#bars do
	local b = bars[I]
	print('barDraw')
	print(B)/>/>
	print(b.x1)
	print(b.y1)
	print(b.x2)
	print(b.y2)
	print(b.c)
	print(b.dark)
	print('')
	paintutils.drawLine(b.x2,b.y2,b.x1,b.y1-b.dark,b.c)
  end
  term.redirect(term.native())
end]]--
function barDraw()
  for I = 1,#bars do
	local b = bars[I]
	disp.setBackgroundColor(b.c)
	--print entire bar
	for y = b.y1,b.y2,1 do
	  disp.setCursorPos(b.x1,y)
	  disp.write(string.rep(" ",b.x2-b.x1+1))
	end
	--erase part
	disp.setBackgroundColor(32768)
	for y = b.y1,(b.y1+b.dark) do
	  for x = b.x1,b.x2 do
		disp.setCursorPos(x,y)
		disp.write(" ")
	  end
	end
  end
end
reactor = {}
function updateReactor()
  --List containing relevant info
  reactor.active = reac.getActive()
  reactor.caseTemp = reac.getCasingTemperature()
  reactor.rod1 = reac.getControlRodLevel(0)
  reactor.rod2 = reac.getControlRodLevel(1)
  reactor.rod3 = reac.getControlRodLevel(2)
  reactor.rod4 = reac.getControlRodLevel(3)
  reactor.rod5 = reac.getControlRodLevel(4)
  print('updateReactor')
  print(reactor.rod1)
  print(reactor.rod2)
  print(reactor.rod3)
  print(reactor.rod4)
  print(reactor.rod5)
  print(reactor.caseTemp)
  print(reactor.rodAll)
  print('')
  reactor.rodAll = (reactor.rod1+reactor.rod2+reactor.rod3+reactor.rod4+reactor.rod5)/5
  reactor.cool = reac.getCoolantAmount()
  reactor.fuel = reac.getFuelAmount()
end
function drawBox()
  for i = 1,#names do
	b = names[i]
	disp.setBackgroundColor(b.c)
	for T = 0,b.ly do disp.setCursorPos(b.x,b.y+T)
	  for E = 1,b.lx do
		disp.write(" ")
	  end
	end
	disp.setCursorPos(b.x,b.y)
	disp.setTextColor(colors.white)
	disp.write(b.n)
  end
  disp.setBackgroundColor(colors.black)
  disp.write(" ")
end
disp.clear()
drawBox()
while true do
  updateReactor()
  barUpdate()
  barDraw()
  sleep(2)
end
Edited on 17 June 2014 - 07:14 PM
wieselkatze #8
Posted 17 June 2014 - 09:23 PM
That is probably, because in brod1, brod2, brod3 etc. there is no x1, but x.

Expected number, number means, that one of the arguments is not a number. In this case b.x1, it's nil.

Just change the x's in your brod tables.
Tmq5521 #9
Posted 17 June 2014 - 09:31 PM
Thanks again didn't notice them