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

Probably really simple

Started by munchmo, 27 December 2012 - 12:44 PM
munchmo #1
Posted 27 December 2012 - 01:44 PM
I can't seem to figure out what I'm missing, but what I want is based on a condition, kill the running program. I tried goto but that doesn't seem to be implemented, using shell.exit() just reboots the turtle. Break gives me errors because it's not in a loop. Is there some way to just stop the current program immediately? Here's the code, I basically wanna get rid of the "goto done" lines with a full break. I'm sure there are more errors in there still.


-- Clearing Terminal
term.clear()
term.setCursorPos(1,1)
-- Setting up Variables
local tArgs = { ... }
local fuel = 1
local torch = 2
local block = 3
local gone = 0
if #tArgs ~= 1 then
-- Missing Length Argument
print("Usage: Branch <length>")
print("Slot 1: Fuel")
print("Slot 2: Torches")
print("Slot 3: Blocks")
goto done
end
local length = tArgs[1]
--Missing Items Check
if (turtle.getItemCount(fuel) < 1 or turtle.getItemCount(torch) < 1 or turtle.getItemCount(block) < 1) then
print("Missing Critical Item")
goto done
end
-- Fuel Checking Function
function tfuel(moved,f)
if turtle.getFuelLevel() < (moved + 10) then
  turtle.select(f)
  if not turtle.refuel(1) then
   print("Need Fuel")
   finished(moved)
  end
end
end
-- Drop Items Function
function dump()
print("Dumping Goods")
for i = 4, 16 do
  turtle.select(i)
  turtle.drop()
end
end
-- Return Function
function finished(m)
print("Returning after " .. m .. " blocks")
turtle.turnRight()
turtle.turnRight()
for i = 0, m do
  turtle.forward()
end
dump()
goto done
end
--Check for Walls
function wallCheck(B)/>
turtle.turnRight()
if not turtle.detect() then
  turtle.select(B)/>
  turtle.place()
end
turtle.turnLeft()
turtle.turnLeft()
if not turtle.detect() then
  turtle.select(B)/>
  turtle.place()
end
turtle.turnRight()
end
for g = 0, (length/2) do
tFuel(gone, fuel)
while turtle.detect() do
  turtle.dig()
  sleep(0.5)
end
turtle.forward()
if not turtle.detectDown() then
  turtle.select(block)
  turtle.placeDown()
end
wallCheck(block)
while turtle.detectUp() do
  turtle.digUp()
  sleep(0.5)
end
turtle.up()
wallCheck(block)
if gone%5 == 0 then
  turtle.select(torch)
  turtle.placeDown()
end
while turtle.detectUp() do
  turtle.digUp()
  sleep(0.5)
end
turtle.up()
wallCheck(block)
if not turtle.detectUp() then
  turtle.select(block)
  turtle.placeUp()
end
while turtle.detect() do
  turtle.dig()
  sleep(0.5)
end
turtle.forward()
wallCheck(block)
if not turtle.detectUp() then
  turtle.select(block)
  turtle.placeUp()
end
turtle.digDown()
turtle.down()
wallCheck(block)
turtle.digDown()
turtle.down()
wallCheck(block)
if not turtle.detectDown() then
  turtle.select(block)
  turtle.placeDown()
end
gone = gone + 2
end
finished(gone)
::done::
Doyle3694 #2
Posted 27 December 2012 - 01:51 PM
goto is actually not in lua 5.1. Though I take the blame on myself because I said so until I got corrected, but an easy fix would be replaceing "goto done" with "error()" and that will hopefully solve your problem.
ChunLing #3
Posted 27 December 2012 - 03:40 PM
Use return, it works with programs.
munchmo #4
Posted 27 December 2012 - 04:06 PM
goto is actually not in lua 5.1. Though I take the blame on myself because I said so until I got corrected, but an easy fix would be replaceing "goto done" with "error()" and that will hopefully solve your problem.

That did it! Thanks so much.
tesla1889 #5
Posted 27 December 2012 - 10:13 PM
Use return, it works with programs.

i use this all the time :D/>