0

For the sake of writing fast turtle programs, I am wondering how, in relation to ticks, is code executed? Is there a tick penalty for executing functions or evaluating other Lua statements in addition to the time it takes the turtle to move? In other words, does it take the same amount of time to do this (assuming the if statements evaluate to false):

turtle.forward()
if turtle.getFuelLevel() == 0 then turtle.refuel() end
turtle.forward()

this:

turtle.forward()
if x < 20 then turtle.refuel() end
turtle.forward()
x = x+1

and

turtle.forward()
turtle.forward()

? Thanks

Edit:

According to Eric, anything that interfaces with Minecraft could take a tick, but operations that are raw Lua will not. In other words, the first example takes 2 extra ticks (in addition to the multi-tick move), the second takes 1 extra tick, and the final has no extra ticks.

bcdan
  • 1,438
  • 1
  • 12
  • 25
  • 1
    Anything that interfaces with minecraft _could_ take a tick. Things like x = x + 1 will not take a tick. You'd need to read the source code for computercraft to know more – Eric Jun 02 '15 at 16:41
  • `turtle.forward`, etc. will block until the operation is complete. Technically, ComputerCraft runs your code in a Lua coroutine, and `turtle.forward` et.al. yields the coroutine with some values to signal ComputerCraft the action it wants to take. ComputerCraft resumes your code when it's done. – Colonel Thirty Two Jun 16 '15 at 13:49
  • So ComputerCraft runs `turtle` and similar commands on the Minecraft schedule, but runs everything else asynchronously to the game? – bcdan Jun 16 '15 at 21:26
  • No. While your code is executing, nothing else in the game happens and MC is totally focused on running your code. If you take too long, ComputerCraft will terminate your program so you don't freeze Minecraft. – Colonel Thirty Two Jun 18 '15 at 18:06
  • So the code is executed between ticks until a tick is needed to complete the operation. – bcdan Jun 18 '15 at 18:48

1 Answers1

0

Usually, the only things that would trigger a sort of tick, or cause a sizable amount of stoppage for a tick to pass by is pulling events (os.pullEvent,os.pullEventRaw,coroutine.yield) turtle movement does of course need a tick to go by, and do call coroutine.yield to pause the script and move.

The thing with technical sides of computercraft is that they are better answered by those who are close to the mod itself and know a lot about it. Not many who frequent SO have that kind of knowledge. If you want to know everything, I would ask on the CC forums about it.

Flamanis
  • 156
  • 5