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

[turtles] How necessary are sleep commands?

Started by Heliomance, 06 February 2013 - 04:34 AM
Heliomance #1
Posted 06 February 2013 - 05:34 AM
I'm writing a program to make a turtle build a structure for me, and currently I have
sleep (0.1)
in between every movement and block place command. How necessary is that? having it in there so much is really annoying to type, and is almost doubling the length of the program. If it's taken out, will the turtle run amok and try and build the thing faster than can be processed, resulting in glitchiness?
theoriginalbit #2
Posted 06 February 2013 - 05:36 AM
the normal use of the sleep function is so that the computer can yield. A computer needs to yield so that other computers can run. However other things can yield too, such as turtle movement, or os.pullEvent, and others. so no it is not required to have sleep after everything. A sleep would be required on code that will be running for about 10 seconds without yielding.
Heliomance #3
Posted 06 February 2013 - 05:42 AM
Yield?

So if the program is a long chain of move - place - move - place, how often should I put in sleep commands?
theoriginalbit #4
Posted 06 February 2013 - 05:48 AM
Yield?

So if the program is a long chain of move - place - move - place, how often should I put in sleep commands?
Yield = Pause running.
In ComputerCraft the computers all don't run at the same time, they run one after the other, so one needs to stop running so another can run.

Never. Moving the turtle yields.
shiphorns #5
Posted 07 February 2013 - 09:17 AM
You don't need to sleep between moves, a typical place where you would want to use sleep is when you're waiting for a condition to happen. For example, suppose you want the turtle to move forward, and to gracefully handle a player or mob getting in the way and wait for him to move. This is the wrong way to do it:

  while not turtle.forward() do end

This is the correct way:

  while not turtle.forward() do sleep(0.5) end

The first bit of code causes the turtle running it to execute in the while loop, checking for the ability to move as quickly as the Lua VM will allow, denying other turtle and CC computers execution time slices. If it remains in the loop long enough (e.g. you stand in front of the turtle for a while) the program will eventually terminate after some number of seconds. The second version adds a half-second sleep, so that the turtle tries forward movement twice per second, but yields the time in between to other turtles/computers. This second version of the code will loop indefinitely, patiently waiting for the turtle to successfully move, without terminating.
Lyqyd #6
Posted 07 February 2013 - 10:38 AM
You don't need to sleep between moves, a typical place where you would want to use sleep is when you're waiting for a condition to happen. For example, suppose you want the turtle to move forward, and to gracefully handle a player or mob getting in the way and wait for him to move. This is the wrong way to do it:

  while not turtle.forward() do end

This is the correct way:

  while not turtle.forward() do sleep(0.5) end

The first bit of code causes the turtle running it to execute in the while loop, checking for the ability to move as quickly as the Lua VM will allow, denying other turtle and CC computers execution time slices. If it remains in the loop long enough (e.g. you stand in front of the turtle for a while) the program will eventually terminate after some number of seconds. The second version adds a half-second sleep, so that the turtle tries forward movement twice per second, but yields the time in between to other turtles/computers. This second version of the code will loop indefinitely, patiently waiting for the turtle to successfully move, without terminating.

Incorrect. Most turtle API calls (including forward()) yield. This code would not cause the problems you describe.
shiphorns #7
Posted 08 February 2013 - 08:21 AM
Incorrect. Most turtle API calls (including forward()) yield. This code would not cause the problems you describe.

OK, that's good to know, regarding the API. So it would only be required if checking one's own functions that do not already have a sleep() built in?

I do see the pattern, even in the rom programs, of including sleep in movement attempting loops. Presumably this is not to avoid termination, but just to reduce CPU usage with unnecessarily frequent checking?
sjkeegs #8
Posted 08 February 2013 - 08:27 AM
Incorrect. Most turtle API calls (including forward()) yield. This code would not cause the problems you describe.
I do see the pattern, even in the rom programs, of including sleep in movement attempting loops. Presumably this is not to avoid termination, but just to reduce CPU usage with unnecessarily frequent checking?
There are a few cases where sleep is added on purpose in move/dig functions.

1. To allow some time for gravel to fall so that the turtle can detect that it's still blocked in that direction.
2. To wait for a bit while performing an attack function to determine if a Mob has moved out of the way, or has been killed.
ChunLing #9
Posted 08 February 2013 - 09:41 AM
There is actually a point in using something like "while not turtle.forward() do sleep(0.4) end", because if the turtle can't move then it returns false immediately rather than waiting a second, so a turtle that sleeps there will wait before trying to move forward again…I don't do this myself though, since the turtle still yields either way.
Angry_Dragonoid #10
Posted 04 February 2016 - 04:28 AM
I came on here looking for difference between o's.sleep() and sleep()
Anavrins #11
Posted 04 February 2016 - 04:37 AM
I came on here looking for difference between o's.sleep() and sleep()
Not much, os.sleep() actually calls sleep() https://github.com/alekso56/ComputercraftLua/blob/master/bios.lua#L582
You're better off using sleep(), it's faster to write, and does one less table lookup.
Angry_Dragonoid #12
Posted 17 February 2016 - 06:39 PM
I came on here looking for difference between o's.sleep() and sleep()
Not much, os.sleep() actually calls sleep() https://github.com/a...r/bios.lua#L582
You're better off using sleep(), it's faster to write, and does one less table lookup.

Thanks!