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

Turtle skipping commands

Started by Delryn, 29 September 2014 - 12:57 PM
Delryn #1
Posted 29 September 2014 - 02:57 PM
Hello,

I wrote a fairly simple script to help build wall sections of a building. The script contains a refueling function, various wall section functions, and the actual script part which splits and parses a command string.

usage: buildWall 3n,e,c,2n,d,2n

This would be interpreted as, build three normal sections, edge, corner, two normals, divider, two normals. It builds a 1x11 vertical section of wall, then moves to the right and down to start the next section.

The turtle seems to build fine the first time, but if I destroy the wall, and have it do the section over again, it will skip commands. Usually after laying 4 blocks or so, it starts to miss move or place orders at random. I'm not 100% sure it only happens when re-building a wall, but that seems to be the case. The wall is being built in a level area, in a mystcraft world.

Do I need to wait for a true/false from a move command before performing a place? Do I need to add sleeps somewhere? I'm not really sure. I haven't been able to find this happening to anyone else.

http://pastebin.com/UZ5pKcLb

I will post some pictures later when I'm back at home of what happens.

Thanks!
Bomb Bloke #2
Posted 30 September 2014 - 06:10 AM
At a glance, I can't see anything that'd specifically and reliably cause the turtle to misbehave on some run-throughs but not others.

However, your lack of variable localisation may have something to do with it. Un-localised variables are defined as "globals", and they'll stay loaded in the turtle's RAM once the script ends - potentially interfering with subsequent attempts to run the script.

If this is the case, then you should be able to confirm it by rebooting the turtle (eg, breaking and replacing the turtle, or holding Ctrl+R) before running the script again. If that reliably solves the problem, it's a stray global variable at fault.

Best practise is to localise pretty much everything. Nothing from your script should stay loaded once it exits.

Another possibility is that the turtle movement commands are failing at random. Under most circumstances, so long as there aren't mobs/unwanted blocks around to prevent it from moving, this is rarely a problem… but ideally yeah, you'd be checking to make sure each movement completes successfully.

Eg, instead of calling t.forward() throughout your script, make a wrapper function:

local function forward()
  repeat until t.forward()
end

… and call that. Do the same for going up/down/back/left/right.
Delryn #3
Posted 30 September 2014 - 09:42 PM
Those are great ideas. I will give them a shot.

Thank you. I'm pretty new to lua and am finding it … not the greatest. But I use what I have.