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

[solved]Turtle variable turn

Started by Engineer, 29 January 2013 - 03:10 AM
Engineer #1
Posted 29 January 2013 - 04:10 AM
Hello, this is what my current code is:

while true do
	local var = nil
	term.clear()
	term.setCursorPos(1,1)
	print("Turn left or right?")
	read = lower.string(read())
	if read == "left" then
		  var = "left"
		  break
	elseif read == "right" then
		   var = "right"
		   break
	end
end

turtle.turn -- Here it has to turn the variable var

This is not the context that I am using, but it sort of is. I need the turtle to turn to var. I know I can use an if statement, but in my code that Im currently writing is an if statement not that usefull. Should I make a function or is it in 1 line possible to turn to var?

If it cant be in one line, I know how I could write the function, but I am trying to avoid this.

Thank you in advance.

NOTE: If this is not clear enough, plese say that, than I can try to define it better.
Kingdaro #2
Posted 29 January 2013 - 05:02 AM
First off, you should probably rename your variable "read" to something else, like input. Overwriting a function like that can lead to some issues in the future.

As for your problem, why not just do this?

local input
repeat
  print 'Turn left or right?'
  input = read()
until input == 'left' or input == 'right'

if input == 'left' then
  turtle.turnLeft()
elseif input == 'right' then
  turtle.turnRight()
end

An alternate method that doesn't use an if statement:

local input
repeat
  print 'Turn left or right?'
  input = read()
until input == 'left' or input == 'right'

local dirs = {
  left = turtle.turnLeft,
  right = turtle.turnRight
}
dirs[input]()

Expanding upon that, you could make yourself a table of operations, and the reading wouldn't check for just left or right, but if the user's input is in the table.

local dirs = {
  left = turtle.turnLeft,
  right = turtle.turnRight,
  up = turtle.up,
  down = turtle.down
}

while true do
  print 'What would you like to do?'
  local input = read():lower()
  if dirs[input] then
    dirs[input]()
    break
  end
end
Engineer #3
Posted 29 January 2013 - 05:13 AM
First off, you should probably rename your variable "read" to something else, like input. Overwriting a function like that can lead to some issues in the future.

As for your problem, why not just do this?

local input
repeat
  print 'Turn left or right?'
  input = read()
until input == 'left' or input == 'right'

if input == 'left' then
  turtle.turnLeft()
elseif input == 'right' then
  turtle.turnRight()
end

An alternate method that doesn't use an if statement:

local input
repeat
  print 'Turn left or right?'
  input = read()
until input == 'left' or input == 'right'

local dirs = {
  left = turtle.turnLeft,
  right = turtle.turnRight
}
dirs[input]()

It should be turtle.turnRight() and turtle.turnLeft() right?

But thank you very much!
Orwell #4
Posted 29 January 2013 - 05:16 AM
* snip *
It should be turtle.turnRight() and turtle.turnLeft() right?
No, it shouldn't. He stores a reference to the functions in the dirs table. Then he calls it with dirs[input]() .