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

Return/break commands not working? [1.12.2]

Started by Flexico, 01 November 2019 - 11:04 PM
Flexico #1
Posted 02 November 2019 - 12:04 AM
I've been playing around on the Fabric 1.14.4 port for a while, and wrote a few turtle programs that so far have worked great. However, when I loaded up my 1.12.2 modpack and downloaded my programs, I'm running into a problem when the turtle hits bedrock. The "move forward" function is supposed to set a global variable "stuck" to "true" and exit the function. However, it doesn't exit the function and instead keeps looping through the "Unbreakable block detected" loop and ignoring the "return" command, and did the same thing when I changed it to use "break" instead. I checked to make sure I had the most recent version, and found out yes I do, but it's an alpha. >< Does anyone know a workaround for this?
Here's a bit of my code:

function fwd(n)
local x,a,b
for x=1, n or 1 do
  refuel()
  stuck = false
  while not turtle.forward() do
   a,b = turtle.dig()
   if b == "Unbreakable block detected" then
	print(B)/> --This prints weird on the forum for some reason
	stuck = true
	print(stuck) --temp
	return
	print(stuck) --temp
   end
  end
  if not stuck then
   update("fwd")
  end
end
end
When it hits bedrock, it prints out "Unbreakable block detected", "true", "true" over and over, showing the "return" command is ignored.
Lupus590 #2
Posted 02 November 2019 - 02:44 AM
is the function fwd being called repeatedly?
Edited on 02 November 2019 - 01:44 AM
Luca_S #3
Posted 02 November 2019 - 08:09 PM
As the return needs to be the last statement in a block Lua is going to interpret
  return
  print(stuck)
end
As returning the return value of print(stuck), which means print(stuck) is executed again.
Other than that I see no reason why the return shouldn't work, please post your whole code.
Exerro #4
Posted 04 November 2019 - 08:58 PM
As the return needs to be the last statement in a block Lua is going to interpret
  return
  print(stuck)
end
As returning the return value of print(stuck), which means print(stuck) is executed again.
Other than that I see no reason why the return shouldn't work, please post your whole code.

Yeah, essentially you're doing return print(stuck) as you might do return 5 or something. The return statement will return from the function though so you must be calling fwd in a loop somewhere.