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

Tree Farm ("ArrayIndexOutOfBoundsException")

Started by Nicolay, 09 July 2014 - 07:00 PM
Nicolay #1
Posted 09 July 2014 - 09:00 PM
Hey.

The script gives me an error when the script is almost done.
Picture of the error: http://gyazo.com/8dd...0c7dd7526856a97

I tried to figure this out myself, and got stuck. The route for the turtle
http://gyazo.com/78a...cf8eb9d329207c3

It suppose to continue running until it gets at the bottom dirt on the picture, and go one block up just to make sure the script works.








local TIN = 10   -- Trees In Line
local BBT = 6  -- Blocks Between Trees
local BM = 0   -- Blocks Moved
local TT = 0   -- Trees Taken
local TR = 5   -- Tree Rounds ( 10 Lines = 5 Rounds )
local RT = 0   -- Rounds Taken
local ST = "Left"  -- Side Turn
function ps()   -- Place Sapling
  TT = TT + 1
  fw()
  fw()
  turtle.turnLeft()
  turtle.turnLeft()
  turtle.place()
  turtle.turnRight()
  turtle.turnRight()
  check()
end
function check()  -- Check status and sends movement
  if TT < TIN then
next()
  end
  if TT == TIN and RT < TR then
	switch()
  elseif RT == TR then
park()
print("PARKED")
  end
end
function switch()  -- Switch lines and rotation side
  for x = 1, 2 do
	fw()
  end

  if ST == "Left" then
	ST = "Right"
RT = RT + 1
	turtle.turnLeft()
	for x = 1, 7 do
   fw()
end
turtle.turnLeft()
for x = 1, 2 do
   fw()
end

  elseif ST == "Right" then
	ST = "Left"
turtle.turnRight()
	for x = 1, 7 do
   fw()
end
turtle.turnRight()
for x = 1, 2 do
   fw()
end

  end
  BM = 0
  TT = 0
  ps()
end

function park()
  turtle.up()
  sleep(2)
end
function next()  -- Move to next tree
  BM = 1
  while BM < BBT do
	fw()
	BM = BM + 1
  end
  ps()
end
function fw()
  repeat
		turtle.attack()
  turtle.dig()
		sleep(0)
  until turtle.forward()
end
for x = 1, 1 do
  ps()
end
johnnic #2
Posted 09 July 2014 - 10:03 PM
Line 134 in bios.lua is defining the sleep command. The code for it is:


function sleep( _nTime )--131
	local timer = os.startTimer( _nTime )--132
repeat--133
  local sEvent, param = os.pullEvent( "timer" )--134
until param == timer--135
end--136

So a sleep command is probably causing it. Check what function the turtle is using when it crashes, and then change the sleep values inside it.
Edited on 09 July 2014 - 08:04 PM
Nicolay #3
Posted 09 July 2014 - 10:22 PM
The turtles crashes after doing the ps (place sapling), and I think it crashes at the check function.
theoriginalbit #4
Posted 10 July 2014 - 10:46 AM
Line 134 in bios.lua is defining the sleep command. The code for it is:

-code snip-

So a sleep command is probably causing it. Check what function the turtle is using when it crashes, and then change the sleep values inside it.
no the problem definitely is not in the sleep, nor is the problem in the bios.

The problem here is with the use of recursion, that is, calling a function from within a function, which then calls the original function, etc etc. Now in Lua when calling functions they go into a 'function stack' this stack can only hold 256 function calls. Therefore what is happening is your code is filling up the function stack, causing it to overflow and give you this error. where the problem is occurring (the first time)

your program starts off by calling the function ps (btw that for loop does nothing, for x = 1, 1 do, means it will run once). From the ps function it runs fine until it calls the check function, the check function will then either call next, switch, or park. Now the park function is okay, but at the end of next and switch functions you call ps again, this is where the recursion starts.

so how do you fix this you might ask? well you make use of loops just like you did in the fw function. perhaps with a variable to stop the program when the turtle is parked, like it currently would.


local running = true

--# your functions, removing the recursive calls

function park()
  turtle.up()
  sleep(2)
  running = false
end

while running do
  ps()
end
Nicolay #5
Posted 10 July 2014 - 02:48 PM
Alright, I understand what you're saying. But I don't understand how I could put it into the mod, as I want the script to be easily adjustable by the numbers of trees, and blocks between each tree each. But I assume I can just make it easier without the adjustments?
johnnic #6
Posted 10 July 2014 - 04:19 PM
-snip-

I wasn't aware of this. Thanks!
Edited by
theoriginalbit #7
Posted 12 July 2014 - 06:39 PM
Alright, I understand what you're saying. But I don't understand how I could put it into the mod, as I want the script to be easily adjustable by the numbers of trees, and blocks between each tree each. But I assume I can just make it easier without the adjustments?
You can still definitely make the script adjustable. As it currently stands your code doesn't do this, nor does the problem have anything to do with making your script adjustable. Simply remove the recursion that I talked about and surround the function call to ps with whatever loop you deem necessary to achieve the goals of your program; this way you can run the ps function as many times as you wish, without getting a stack overflow 'caused by recursive calls.