Jump to content




Return/break commands not working? [1.12.2]


3 replies to this topic

#1 Flexico

  • Members
  • 25 posts

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.

#2 Lupus590

  • Members
  • 2,029 posts
  • LocationUK

Posted 02 November 2019 - 02:44 AM

is the function fwd being called repeatedly?

Edited by Lupus590, 02 November 2019 - 02:44 AM.


#3 Luca_S

  • Members
  • 407 posts
  • LocationGermany

Posted 02 November 2019 - 08:09 PM

Quote

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.

#4 Exerro

  • Members
  • 801 posts

Posted 04 November 2019 - 08:58 PM

View PostLuca_S, on 02 November 2019 - 08:09 PM, said:

Quote

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users