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

JavaOutOfBounds Error after a code running in loop

Started by BionicBear, 22 February 2013 - 05:15 AM
BionicBear #1
Posted 22 February 2013 - 06:15 AM

local Mon0 = "back"
local Sct = "Security:"
local Err0 = "Offline"
local Err1 = "Online"
local Rdstn = "left"
local status = SecStatus
local Redstone1 = 5
function Mon0Stat(side, text0, status, times)
if peripheral.isPresent(side) then
  term.redirect(peripheral.wrap(side))
  for i = 1, times do
					    local cx, cy = term.getCursorPos()
					    write(status)
					    term.setCursorPos(cx, cy)
--   term.setTextColor(colours.red) commented due to not really needed
					    os.sleep(1)
   term.setTextColor(colours.white)
					    term.clearLine()
   term.setCursorPos(1,1) --Resets Cursor position to start to avoid Marquee Effect
   write(text0)
   local Err0 = redstone.getInput(Rdstn)
   if Err0
	 then
    term.setTextColor(colours.red)
    doorcont(true) else --if its powered the color is red and Opens doors
    term.setTextColor(colours.green)
    doorcont(false) --if its not powered its green and closes doors
   end
   os.sleep(1)
   SecStatus(Rdstn) --Checks for redstone input change
 
			    end
  os.sleep(10)
  os.reboot()
			    term.restore()
	    end
end
function doorcont(boolean)
redstone.setOutput("top", boolean)
term.restore()
end
function SecStatus(side)
  if  redstone.getInput(side)
   then
    Mon0Stat(Mon0, Sct, Err0, 1) else
    Mon0Stat(Mon0, Sct, Err1, 1)
   end
  term.restore()
end
SecStatus(Rdstn)

This Code, Wrote by myself, works for a shortwhile but then gives a java error OutofBounds error message on the monitor…is there anyway i can fix this?
LBPHacker #2
Posted 22 February 2013 - 06:18 AM
You call SecStatus inside Mon0Stat and Mon0Stat inside SecStatus too. I don't know what's the code supposed to do, but this is what kills Lua. It's like calling foo after declaring it like this:
 function foo() foo() end 
Lyqyd #3
Posted 22 February 2013 - 06:22 AM
This is a cross-recursion stack overflow. Look for information about stack overflows in the Tutorials section.
BionicBear #4
Posted 22 February 2013 - 08:28 AM
Ahh okay not really looked at the tutorials.
You call SecStatus inside Mon0Stat and Mon0Stat inside SecStatus too. I don't know what's the code supposed to do, but this is what kills Lua. It's like calling foo after declaring it like this:

Its a way i got the computer to continuously check for redstone input and if it changes it changes the output
Lyqyd #5
Posted 22 February 2013 - 08:30 AM
This is a case where you should be using loops. Check out the tutorial I mentioned, it has some information about this sort of thing.
BionicBear #6
Posted 22 February 2013 - 08:37 AM
Would

while true do
 redstone.getinput(side, true)
end



be a functional replacement?
Lyqyd #7
Posted 22 February 2013 - 08:58 AM
Here is your code, but made into a actual loop and cleaned up a little bit.


local mon = false
for _, side in pairs(rs.getSides()) do
  if peripheral.isPresent(side) and peripheral.getType(side) == "monitor" then
    mon = peripheral.wrap(side)
  end
end
if not mon then return else term.redirect(mon) end

while true do
  term.setCursorPos(1, 1)
  term.clearLine()
  term.setTextColor(colors.white)
  term.write("Security: ")
  term.setTextColor(rs.getInput(Rdstn) and colors.red or colors.green) --ternary operation.
  term.write(rs.getInput(Rdstn) and "Offline" or "Online")
  rs.setOutput("top", rs.getInput(Rdstn))
  os.pullEvent("redstone")
end
BionicBear #8
Posted 22 February 2013 - 09:16 AM
-snip-

Why thanks, I've only been attempting to learn lua since yesterday. You made all my work obsolete, Will be using this for now . But i need to learn more!