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

question : why does my turtle stop halfway it's script ?

Started by Rawrzor, 02 August 2014 - 08:28 PM
Rawrzor #1
Posted 02 August 2014 - 10:28 PM
forgot to paste the code inbetween the code markers most likely that one will get rejected thus writing a new post (please delete the old post)

So first off i'll start off by saying i'm quit new to coding as a whole thus most likely made thing allot more complicated then it should be.

I'm trying to make a turtle replant my farm. This farm is bigger then one stack of seeds can fill and it's worth mentioning that i'm still at CC 1.5. So in the end i made something that constantly checks if his slots are still filled and if not switches the slot + it walks a preset route.

Now somehow mid program the turtle stops walking forward in a for i = 1,18,1 do turtle.forward() end. Now i think i made somesort of basic mistake while creating this and it feels as if the rest of the program runs to fast without waiting for the other parts to finish.

i have tried adding in sleep(1) commands here and there to see if it would make a difference, but it seems it made it only a little better.
Any help in this would be greatly appreciated.

for the code


local function farmbot()

local function detectslot()

turtle.select(2)
x = 2
z = 3

while true do

if turtle.getItemCount(x) < 1 then

turtle.select(z)

x = x + 1
z = z + 1

else

break

end

end

end

--- ending detectslot

detectslot()
sleep(1)

for i = 1,6,1 do

turtle.forward()
turtle.placeDown()

detectslot()
sleep(1)

end

turtle.turnRight()
turtle.forward()
turtle.placeDown()


detectslot()
sleep(1)

turtle.turnRight()

detectslot()
sleep(1)


for i = 1,5,1 do

turtle.forward()
turtle.placeDown()


detectslot()
sleep(1)

end

turtle.turnLeft()
turtle.forward()
turtle.placeDown()


detectslot()
sleep(1)

turtle.turnLeft()

for i = 1,5,1 do

turtle.forward()
turtle.placeDown()


detectslot()
sleep(1)

end

turtle.up()
turtle.turnLeft()

for i = 1,2,1 do

turtle.forward()
end

turtle.turnRight()

end

------- ending farmbot

for r = 1,4,1 do

for i = 1,3,1 do

farmbot()

end

turtle.turnRight()

for i = 1,4,1 do

turtle.forward()
sleep(1)

end

turtle.turnRight()

for i = 1,18,1 do		[color=#ff0000]<---- weirdly enough it stops mid way this line altho it keeps doing left right commands[/color]

turtle.forward()
sleep(1)
end

for i = 1,2,1 do

turtle.turnRight()
end

for i = 1,3,1 do

turtle.down()
end
end


Sorry if it looks messy, but this is the best i could do for now.
Edited on 02 August 2014 - 09:08 PM
hilburn #2
Posted 02 August 2014 - 11:43 PM
Firstly, you really should indent your code to improve readility.

Secondly, you are defining a function within a function which… honestly I don't know if it might be one of those weird Lua things that works but don't do that


local function function()
  --#do stuff
end
local function function2()
  --#do stuff
  function1()
  --#do more stuff
end

If it is still doing left/right commands you haven't given it enough fuel
natedogith1 #3
Posted 02 August 2014 - 11:54 PM
Secondly, you are defining a function within a function which… honestly I don't know if it might be one of those weird Lua things that works but don't do that

Defining Lua functions in Lua functions is fine, and I believe is actually used in some of the pre-installed programs actually do that. The thing is that you just need to make sure you're only accessing the functions inside their scope.
hilburn #4
Posted 03 August 2014 - 01:29 AM
Defining Lua functions in Lua functions is fine, and I believe is actually used in some of the pre-installed programs actually do that. The thing is that you just need to make sure you're only accessing the functions inside their scope.

Fair enough, but seeing it still makes my skin crawl, there's no reason why you would need to nest your functions like that (at least in this application, and it is taking some real mental gymnastics to come up with any where it would be particularly useful
Rawrzor #5
Posted 03 August 2014 - 02:56 AM
Firstly, you really should indent your code to improve readility.

Secondly, you are defining a function within a function which… honestly I don't know if it might be one of those weird Lua things that works but don't do that


local function function()
  --#do stuff
end
local function function2()
  --#do stuff
  function1()
  --#do more stuff
end

If it is still doing left/right commands you haven't given it enough fuel

omg i can't believe i spend the whole day looking for a fail somewhere in the code and it simply was because the turtle didn't have enough fuel -.- . Somehow i'm glad it was not the code itself, but still i feel incredibly stupid xD. Thanks allot for the help and here is the newly dented code to stop making your skin crawl :P/>.

edit: Huh … i dented it in notepad ++ but somehow it didn't do it here … hmmmm oh well sorry!



local function detectslot()

turtle.select(2)
x = 2
z = 3

while true do
if turtle.getItemCount(x) < 1 then
turtle.select(z)

x = x + 1
z = z + 1

else

break

end

end

end

--- ending detectslot

local function farmbot()

detectslot()
sleep(0.5)

for i = 1,6,1 do

turtle.forward()
turtle.placeDown()

detectslot()
sleep(0.5)

end

turtle.turnRight()
turtle.forward()
turtle.placeDown()


detectslot()
sleep(0.5)

turtle.turnRight()

detectslot()
sleep(0.5)


for i = 1,5,1 do

turtle.forward()
turtle.placeDown()


detectslot()
sleep(0.5)

end

turtle.turnLeft()
turtle.forward()
turtle.placeDown()


detectslot()
sleep(0.5)

turtle.turnLeft()

for i = 1,5,1 do

turtle.forward()
turtle.placeDown()


detectslot()
sleep(0.5)

end

turtle.up()
turtle.turnLeft()

for i = 1,2,1 do

turtle.forward()
end

turtle.turnRight()

end

---- ending farmbot

for i = 1,4,1 do

for i = 1,3,1 do

farmbot()

end

turtle.turnRight()

for i = 1,4,1 do

turtle.forward()

end

turtle.turnRight()

for i = 1,18,1 do

turtle.forward()
end

for i = 1,2,1 do

turtle.turnRight()
end

for i = 1,3,1 do

turtle.down()
end
end

Edited on 03 August 2014 - 07:59 AM
hilburn #6
Posted 03 August 2014 - 01:49 PM
Yeah the code tags don't recognise tabs from Np++, it's very frustrating.

The number of times I've had turtle script fails due to running out of fuel.. happens to us all (so long as you don't play on easy-mode, infinite fuel settings) if you follow my pattern it will be about 3 months where you will be very careful about it and then you'll do it again and feel like an asshat
LeonTheMisfit #7
Posted 03 August 2014 - 06:02 PM
Fair enough, but seeing it still makes my skin crawl, there's no reason why you would need to nest your functions like that (at least in this application, and it is taking some real mental gymnastics to come up with any where it would be particularly useful

You could use it for access control. Say you want to break a function down into multiple functions but only want them available INSIDE that function. (insert Inception / XZibit joke here) Just an idea, personally I've never done it, but it could be a use for it.

Although, if you read about the practice on StackOverflow, you'll see that doing this causes a serious hit to performance, so it should probably be avoided anyway.
Edited on 03 August 2014 - 04:05 PM
Lyqyd #8
Posted 04 August 2014 - 03:28 PM
When pasting code, don't put anything in the input box that pops up when you click on the code option. Hit OK on that box, then paste the code directly in to the main editing box between the code tags.
flaghacker #9
Posted 04 August 2014 - 03:39 PM
When pasting code, don I't put anything in the input box that pops up when you click on the code option. Hit OK on that box, then paste the code directly in to the main editing box between the code tags.

Why is that?
Lyqyd #10
Posted 04 August 2014 - 04:09 PM
So the indentation is preserved. Comments earlier in the topic remarked on indentation being stripped away. I've only heard of this being an issue when attempting to use the input box that pops up.