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?
[turtles] How necessary are sleep commands?
#1
Posted 06 February 2013 - 05:34 AM
#2
Posted 06 February 2013 - 05:36 AM
#3
Posted 06 February 2013 - 05:42 AM
So if the program is a long chain of move - place - move - place, how often should I put in sleep commands?
#4
Posted 06 February 2013 - 05:48 AM
Heliomance, on 06 February 2013 - 05:42 AM, said:
So if the program is a long chain of move - place - move - place, how often should I put in sleep commands?
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.
#5
Posted 07 February 2013 - 09:17 AM
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.
#6
Posted 07 February 2013 - 10:38 AM
shiphorns, on 07 February 2013 - 09:17 AM, said:
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.
#7
Posted 08 February 2013 - 08:21 AM
Lyqyd, on 07 February 2013 - 10:38 AM, said:
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?
#8
Posted 08 February 2013 - 08:27 AM
shiphorns, on 08 February 2013 - 08:21 AM, said:
Lyqyd, on 07 February 2013 - 10:38 AM, said:
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.
#9
Posted 08 February 2013 - 09:41 AM
#10
Posted 04 February 2016 - 04:28 AM
#11
Posted 04 February 2016 - 04:37 AM
Angry_Dragonoid, on 04 February 2016 - 04:28 AM, said:
You're better off using sleep(), it's faster to write, and does one less table lookup.
#12
Posted 17 February 2016 - 06:39 PM
Anavrins, on 04 February 2016 - 04:37 AM, said:
Angry_Dragonoid, on 04 February 2016 - 04:28 AM, said:
You're better off using sleep(), it's faster to write, and does one less table lookup.
Thanks!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











