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

[Lua] [Error] name:4: Too long without yielding

Started by legobmw99, 17 March 2013 - 09:14 AM
legobmw99 #1
Posted 17 March 2013 - 10:14 AM
I keep getting the same error when using the XP turtle from misc. peripherals
Code is below, thanks for any help and/or fixes

m=peripheral.wrap("right")
m.setAutoCollect()
while true do
  if (m.getLevels()>=30) then
	turtle.select(1)
	turtle.transferTo(16,1)
	turtle.select(16)
	m.enchant(30)
	turtle.drop()
  end
end
Lyqyd #2
Posted 17 March 2013 - 12:09 PM
Split into new topic.

When your turtle has fewer than 30 levels, it just busy loops, doing nothing and checking the levels. Outside the if statement, add a sleep(10) or something so that it actually yields.
legobmw99 #3
Posted 17 March 2013 - 04:10 PM
Split into new topic.

When your turtle has fewer than 30 levels, it just busy loops, doing nothing and checking the levels. Outside the if statement, add a sleep(10) or something so that it actually yields.
Could you give an example of something that would yield?
Thanks!
theoriginalbit #4
Posted 17 March 2013 - 04:14 PM
he did give an example….

but here is some more
  • sleep
  • os.pullEvent
  • os.pullEventRaw
  • turtle functions that involve the turtle moving
basically he just means have the turtle sleep at the end of the while loop… i would probs recommend to put

while true do
  if (m.getLevels()>=30) then
	-- existing code
  else
	sleep(10)
  end
end
that way when the level is greater than 30 it just sits there and waits
legobmw99 #5
Posted 17 March 2013 - 04:16 PM
he did give an example….

but here is some more
  • sleep
  • os.pullEvent
  • os.pullEventRaw
  • turtle functions that involve the turtle moving
basically he just means have the turtle sleep at the end of the while loop… i would probs recommend to put

while true do
  if (m.getLevels()>=30) then
	-- existing code
  else
	sleep(10)
  end
end
that way when the level is greater than 30 it just sits there and waits
Ah okay, got that sorted out.
How about for programs that shouldnt wait? Like turtles that are just

while true do
turtle.attack()
end
theoriginalbit #6
Posted 17 March 2013 - 04:22 PM
I cannot remember 100%, but I'm pretty sure that turtle.attack will yield, so it won't error out, just constantly attack and probs cause some lag.
as for programs that you don't want to wait, you can use pull event loops or just simply sleep(0) depending on which is appropriate for the given program
Lyqyd #7
Posted 17 March 2013 - 04:27 PM
Yeah, the turtle commands yield internally, so they don't cause the too long without yielding error.

Really, though, this script does not need to be constantly running. A ten-second sleep after the if block would be just fine for it. Unless you're worried that it will gain more than thirty levels in ten seconds?
legobmw99 #8
Posted 17 March 2013 - 04:30 PM
I cannot remember 100%, but I'm pretty sure that turtle.attack will yield, so it won't error out, just constantly attack and probs cause some lag.
as for programs that you don't want to wait, you can use pull event loops or just simply sleep(0) depending on which is appropriate for the given program
Neither turtle.attack nor sleep(0) appear to yield, as I just got the same error with

while true do
turtle.attack()
sleep(0)
end
theoriginalbit #9
Posted 17 March 2013 - 04:31 PM
Ok I just looked at the turtle api again to refresh my memory and it seems that all turtle functions yield, i always thought that there were some that didn't, but it seems that they all do.

EDIT: uhhhhhh. then there is something else going wrong with your code, sleep definitely does yield!
Lyqyd #10
Posted 17 March 2013 - 04:58 PM
I cannot remember 100%, but I'm pretty sure that turtle.attack will yield, so it won't error out, just constantly attack and probs cause some lag.
as for programs that you don't want to wait, you can use pull event loops or just simply sleep(0) depending on which is appropriate for the given program
Neither turtle.attack nor sleep(0) appear to yield, as I just got the same error with

while true do
turtle.attack()
sleep(0)
end

Is that the whole and only code you're using? I suspect that it isn't, if you're still getting that error.
legobmw99 #11
Posted 17 March 2013 - 05:19 PM
I cannot remember 100%, but I'm pretty sure that turtle.attack will yield, so it won't error out, just constantly attack and probs cause some lag.
as for programs that you don't want to wait, you can use pull event loops or just simply sleep(0) depending on which is appropriate for the given program
Neither turtle.attack nor sleep(0) appear to yield, as I just got the same error with

while true do
turtle.attack()
sleep(0)
end

Is that the whole and only code you're using? I suspect that it isn't, if you're still getting that error.
That is the whole and only code on the entire turtle besides the rom
Lyqyd #12
Posted 17 March 2013 - 05:41 PM
That code should not cause a too long without yielding error on its own. Has anyone modified the rom? Try changing the sleep to a sleep(10) and see if that fixes the issue.