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

Please help! Why is my score checking program is returning errors that don't make sense

Started by austinv11, 27 June 2013 - 10:55 AM
austinv11 #1
Posted 27 June 2013 - 12:55 PM
I wrote a score checking program based on redstone input, but when I run it says "204" after around one cycle of the program running.

code:

local i=75
local lava="s"
local r="off"
local red="off"
local own="s"
local off=false
local back
local right

function rcheck()
  right=redstone.getInput("right")
  back=redstone.getInput("back")
  if back==true then
    r="on"
    own="n"
  elseif back==false then
    r="off"
    own="s"

  elseif right==true then
    off=true
    end
  end

function own()
  rcheck()

  if r~=red and own=="n" then
    rednet.broadcast("lava(n)")
    red=r
    sleep(30)
    own()

  elseif r~=red and own=="s" then
    rednet.broadcast("lava(s)")
    red=r
    sleep(3)


  elseif off==true then
    for i=1, 75, 1 do
	  rednet.broadcast("lava(o)")
	  sleep(1)
	  end

  else
    sleep(1)
    own()
    end
  end

while true do
  own()
  end
Lyqyd #2
Posted 27 June 2013 - 01:47 PM
Split into new topic.
nolongerexistant #3
Posted 27 June 2013 - 01:56 PM
I wrote a score checking program based on redstone input, but when I run it says "204" after around one cycle of the program running.

code:

-snip-

The variable "own" has the same name as the function own.
austinv11 #4
Posted 27 June 2013 - 03:50 PM
I wrote a score checking program based on redstone input, but when I run it says "204" after around one cycle of the program running.

code:

-snip-

The variable "own" has the same name as the function own.

ohh, well that was a huge derp, i had no idea what was going on lol, thanks!
austinv11 #5
Posted 27 June 2013 - 04:42 PM
now its saying" bios:123 : vm error: java.lang.arrayIndexOutOfBoundsException: 256"
trakos #6
Posted 27 June 2013 - 05:47 PM
Is it happening after some time, or instantly? I'd guess that it is too big stack error, though I'm not sure. Meaning, too much recurrence.
When you call a function, the interpreter must remember when it has to come back when the function will finish. Thus, it is much better to make a loop than call the function from within itself, if there's no gain in it (there are many algorithms when recurrence simplifies a lot, but your code is not such an example). So, you don't have to call own() inside own(), because when it's finished it is called anyway.

BTW, you might want to paste your code again, because maybe you've changed something important ;)/>.