2 posts
Posted 02 April 2013 - 11:14 AM
Hello I seem to have a problem with my code.
Error:
bios:124: vm error: java.lang.ArrayIndexOutOfBoundsException: 256
function tree()
if turtle.compare() == false then
turtle.dig()
turtle.forward()
while turtle.detectUp() do
turtle.digUp()
turtle.up()
end
while not turtle.detectDown() do
turtle.down()
end
turtle.back()
turtle.place()
end
tree()
end
tree()
1522 posts
Location
The Netherlands
Posted 02 April 2013 - 11:17 AM
By doing this:
function x()
x()
end
x()
with this code you have recursion, afunction that calls it selfs. After ~ 256 times you get that error. Try:
while true do
--code
End
Edit: my phone decided to post an unfinished post :/
1688 posts
Location
'MURICA
Posted 02 April 2013 - 11:17 AM
A function calling itself is a bad way of looping a program. Use a while loop.
while true do
if turtle.compare() == false then
turtle.dig()
turtle.forward()
while turtle.detectUp() do
turtle.digUp()
turtle.up()
end
while not turtle.detectDown() do
turtle.down()
end
turtle.back()
turtle.place()
end
end
68 posts
Posted 02 April 2013 - 11:18 AM
You are causing a stack overflow by calling the function from itself:
function tree()
--stuff
tree()
end
You should remove that function call from
inside the function. You're calling it after that.
Edit: If you want the turtle to stay on a loop, do this:
function tree()
--do stuff
end
while true do
tree();
end
Also, I think the trees may not grow if you are parking the turtle immediately adjacent to the sapling, the turtle might count as an occupied block.
2 posts
Posted 02 April 2013 - 04:48 PM
I ended up just puting a while true loop to enclose the entire function. though thanks for the help now i partialy under stand what is the problem when i see an error like that.
Also, I think the trees may not grow if you are parking the turtle immediately adjacent to the sapling, the turtle might count as an occupied block.
If you look in the mincraft wiki about tree farming they have a simple tree farm with dirt on all sides of the sapling
34 posts
Location
Russia, Krasnodar
Posted 02 April 2013 - 07:15 PM
recursion - not bad solution, but not so many times… if you do recursion you must think twicely… or more time
7508 posts
Location
Australia
Posted 02 April 2013 - 07:17 PM
recursion - not bad solution, but not so many times… if you do recursion you must think twicely… or more time
It really is a bad solution… there are no circumstances ever where you can and should use it as an infinite loop… there must always be a way out of the recursion!
68 posts
Posted 02 April 2013 - 07:23 PM
You can probably count in one hand the cases in which recursion is an acceptable way to solve a problem. In a great majority of the cases, if you use recursion, you're doing it wrong.
7508 posts
Location
Australia
Posted 02 April 2013 - 07:27 PM
You can probably count in one hand the cases in which recursion is an acceptable way to solve a problem.
Yep. I can only count 3.
1688 posts
Location
'MURICA
Posted 02 April 2013 - 07:35 PM
I remember a recursive factorial function being listed in Programming in Lua 5.2. Something like this, except with if..else.
function fact(n)
return n * (n > 1 and fact(n - 1) or 1)
end
But that would overflow with larger numbers. Though it's nice and small.
EDIT: I just tested, and a stack overflow happens at about 20000. Waaay before that point, though, it just starts printing 'inf'
34 posts
Location
Russia, Krasnodar
Posted 02 April 2013 - 07:44 PM
200000!
Man, it's really big number and greater than longInt