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

do ... end

Started by KingofGamesYami, 17 July 2014 - 03:01 PM
KingofGamesYami #1
Posted 17 July 2014 - 05:01 PM
Does do … end count as a loop in lua? I want to easily exit from many points in a certain block in my script, and want to use break to do so. I know I could set up a single run loop, but I decided I might as well check if this will work.

do
  --#stuff
  if <condition> then
    break
  end
  --#more stuff
  --#repeat
end
--#do stuff when done

As backup, I can easily do

local go = true
while go do
  --#stuff
  if condition then
    break
  end
  --#more stuff
  --#repeat
  go = false
end
theoriginalbit #2
Posted 17 July 2014 - 05:04 PM
nope. unfortunately do … end cannot be broken nor returned from. It is simply a way to limit scope. For example:


local foo
do
  local bar = "Hello World!"
  function foo()
    print(bar)
  end
end

foo() --# works
print( bar ) --# will print nothing, there's no variable bar that is in this or any parent scope

using break you'd get an error along the lines of 'not inside loop' and using return will exit whatever code block it is in, whether that's a function or the program.
Edited by
KingofGamesYami #3
Posted 17 July 2014 - 05:05 PM
Dang… Guess I'll go with single use while loops.
theoriginalbit #4
Posted 17 July 2014 - 05:08 PM
What's your use case? there may be another alternative for you.

btw using a single for loop might be nicer to read; in the event there's no alternative

for i = 1, 1 do
  --# stuff
  if condition then
    break
  end
  --# stuff
end
KingofGamesYami #5
Posted 17 July 2014 - 05:15 PM
Inside the ore functions of this script I've been working on
Spoiler

--[[
Slot 1: Stone
Slot 2: Dirt
Slot 3: Sand
Slot 4: Gravel
Slot 16: Fuel
]]--

local function notwaste( func )
	turtle.select( 1 )
	if func() then
		return false
	end
	turtle.select( 2 )
	if func() then
		return false
	end
	turtle.select( 3 )
	if func() then
		return false
	end
	turtle.select( 4 )
	if func() then
		return false
	end
	return true
end

local function oreDown()
	turtle.digDown()
	turtle.down()
	local go = true
	while go do
		if not ok then
			break
		end
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnLeft()
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnRight()
		turtle.turnRight()
		if notwaste( turtle.compare ) then
			ore()
		end
		if notwaste( turtle.compareUp ) then
			oreUp()
		end
		if notwaste( turtle.compareDown ) then
			oreDown()
		end
		go = false
	end
	turtle.up()
end

local function oreUp()
	turtle.digUp()
	turtle.up()
	local go = true
	while go do
		if not ok then
			break
		end
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnLeft()
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnRight()
		turtle.turnRight()
		if notwaste( turtle.compare ) then
			ore()
		end
		if notwaste( turtle.compareUp ) then
			oreUp()
		end
		if notwaste( turtle.compareDown ) then
			oreDown()
		end
		go = false
	end
	turtle.down()
end

local function ore()
	turtle.dig()
	turtle.forward()
	local go = true
	while go do
		if not ok then
			break
		end
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnLeft()
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnRight()
		turtle.turnRight()
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnLeft()
		if notwaste( turtle.compareUp ) then
			oreUp()
		end
		if notwaste( turtle.compareDown ) then
			oreDown()
		end
		go = false
	end
	turtle.back()
end

local function branch()
	local gone = 0
	for i = 1, 15 do
		while not turtle.forward() do turtle.dig() end
		gone = gone + 1
		if not ok then
			break
		end
		turtle.turnLeft()
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnRight()
		turtle.turnRight()
		if notwaste( turtle.compare ) then
			ore()
		end
		turtle.turnLeft()
		if notwaste( turtle.compareUp ) then
			oreUp()
		end
		if notwaste( turtle.compareDown ) then
			oreDown()
		end
	end
	for i = 1, gone do
		turtle.back()
	end
end

local ok = true

local function main()
	local gone = 0
	while ok do
		while not turtle.forward() do turtle.dig() end
		gone = gone + 1
		turtle.turnLeft()
		branch()
		turtle.turnRight()
		turtle.turnRight()
		branch()
		turtle.turnLeft()
		for i = 1, 5 do
			while not turtle.forward() do turtle.dig() end
			gone = gone + 1
		end
	end
	--not ok, return to base
	repeat
		if turtle.back() then
			gone = gone - 1
		end
	until gone == 0
end

local function findMaxLevel()
	local level = turtle.getFuelLevel()
	if turtle.getItemCount( 16 ) > 1 then
		turtle.select( 16 )
		turtle.refuel( 1 )
		local fuelAmount = turtle.getFuelLevel() - level
		return turtle.getItemCount( 16 ) * fuelAmount + turtle.getFuelLevel()
	else
		return turtle.getFuelLevel()
	end
end

local function isOk()
	local okLevel = findMaxLevel() / 3
	while true do
		if turtle.getFuelLevel() < okLevel then
			if turtle.getItemCount( 16 ) > 0 then
				turtle.select( 16 )
				ok = turtle.refuel( 5 )
			else
				ok = false
			end
		else
			local hasSpace = false
			for i = 1, 16 do
				if turtle.getItemCount( i ) == 0 then
					hasSpace = true
				end
			end
			if not hasSpace then
				ok = false
			else
				sleep( 2 )
			end
		end
	end
end

parallel.waitForAny( isOk, main )
for i = 5, 15 do
	turtle.select( i )
	turtle.dropDown()
end
Cranium #6
Posted 17 July 2014 - 05:16 PM
You could also shorten that with a repeat until [condition] loop.

repeat
stuffToDo()
until condition
Edited on 17 July 2014 - 03:17 PM
KingofGamesYami #7
Posted 17 July 2014 - 05:19 PM
Lol single use loop:

repeat
stuff()
until false

Thanks for the help.
theoriginalbit #8
Posted 17 July 2014 - 05:24 PM
You have another problem, due to the fact that you define the local variable 'ok' after you use it in the 'ore' functions
KingofGamesYami #9
Posted 17 July 2014 - 09:09 PM
I actually had several more problems. I think I fixed most of them, so I'm testing it now. http://pastebin.com/ZpKSLTgW